1.首先到GridControl控件设计里设置属性Repository (In-place EditorRepository) 如下图(CheckEdit可以不添加,这是本人根据自己需求添加的):
主要设置两个两个属性 NullText,Name
2.绑定数据
关键代码如下:
DataTable dt = dbHelp.GetDataSql("select TS_NO,TS_NAME ,TS_LONGITUDE,TS_LATITUDE,TS_RANK from dbo.LPTE_TS");
grStation.DataSource = null;
gvStation.Columns.Clear();
grStation.DataSource = dt;
gvStation.Columns["TS_NO"].Visible = false;
gvStation.Columns["TS_NAME"].Visible = true;
gvStation.Columns["TS_NAME"].Caption = "名称";
gvStation.Columns["TS_NAME"].VisibleIndex = 1;
gvStation.Columns["TS_RANK"].Visible = true;
gvStation.Columns["TS_RANK"].Caption = "等级";
gvStation.Columns["TS_RANK"].VisibleIndex = 2;
gvStation.Columns["TS_LONGITUDE"].Caption = "经度";
gvStation.Columns["TS_LATITUDE"].Caption = "纬度";
GridColumn addLinkHiper = new GridColumn();
addLinkHiper.Caption = "新增";
addLinkHiper.Visible = true;
addLinkHiper.ColumnEdit = stationAdd;
gvStation.Columns.Add(addLinkHiper);
addLinkHiper.VisibleIndex = 5;
GridColumn editLinkHiper = new GridColumn();
editLinkHiper.Caption = "编辑";
editLinkHiper.Visible = true;
editLinkHiper.ColumnEdit = stationEdit;
gvStation.Columns.Add(editLinkHiper);
editLinkHiper.VisibleIndex = 6;
GridColumn delLinkHiper = new GridColumn();
delLinkHiper.Caption = "删除";
delLinkHiper.Visible = true;
delLinkHiper.ColumnEdit = stationDel;
gvStation.Columns.Add(delLinkHiper);
delLinkHiper.VisibleIndex = 7;
gvStation.BestFitColumns();
以上数据已全部绑定完成
3.触发事件
在上图中点击事件Click,新增不多说,弹出新窗体即可
//编辑
private void stationEdit_Click(object sender, EventArgs e)
{
int[] selectRows = gvStation.GetSelectedRows();
//赋值
int tsNo = Convert.ToInt32(gvStation.GetRowCellValue(selectRows[0], "TS_NO")); //TS_NO是控件列名
//具体操作因人而异
......
}
//删除
private void stationDel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定删除所选数据?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
int[] selectRows = gvStation.GetSelectedRows();
//赋值
int tsNO = Convert.ToInt32(gvStation.GetRowCellValue(selectRows[0], "TS_NO"));
//写sql语句执行删除操作就可以了。
}
}
最后贴一张效果图: