您的位置:首页 > 其它

devexpress.XtraGrid.GridControl 实现多选

2013-01-11 17:30 465 查看
转:http://www.cnblogs.com/jlfood/

添加一列,FieldName为 "check",将ColumnEdit 设置为 复选框 样式。

将要绑定的DataTable添加列 "check",Type 为 bool。

绑定DataTable到GridControl。

获取: string value = gridview.GetDataRow(i)["check"].toString();

value == "true" || "" ("false")

设置为多选

gridView1 .OptionsSelection.MultiSelect = true;

gridView1 .OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;

测试的例子如下:

给gridcontrol添加数据

view plaincopy to clipboard

string strConn = "###";

OracleConnection oconn = new OracleConnection(strConn);

string strComm = "select CITY_NAME,DISTRICT_NAME from CC_COMPLAINT_POINT";

OracleDataAdapter oda = new OracleDataAdapter(strComm, oconn);

DataSet ds = new DataSet();

try

{

oda.Fill(ds, "cx");

ds.Tables["cx"].Columns.Add("check",System.Type.GetType("System.Boolean"));

gridControl1.DataSource = ds.Tables["cx"];

//Rel.DataSource = ds.Tables["cx"];

//Rel.DisplayMember = "DISTRICT_NAME";

//Rel.ValueMember = "CITY_NAME";

}

catch(Exception ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

oconn.Close();

}

[c-sharp] view plaincopy

string strConn = "###"; OracleConnection oconn = new OracleConnection(strConn); string strComm = "select CITY_NAME,DISTRICT_NAME from CC_COMPLAINT_POINT"; OracleDataAdapter oda = new OracleDataAdapter(strComm, oconn); DataSet ds = new DataSet(); try { oda.Fill(ds, "cx"); ds.Tables["cx"].Columns.Add("check",System.Type.GetType("System.Boolean")); gridControl1.DataSource = ds.Tables["cx"]; //Rel.DataSource = ds.Tables["cx"]; //Rel.DisplayMember = "DISTRICT_NAME"; //Rel.ValueMember = "CITY_NAME"; } catch(Exception ex) { MessageBox.Show(ex.ToString()); } finally { oconn.Close(); }

点击测试check按钮响应如下事件(获取被check的数据)

view plaincopy to clipboard

private void buttonX3_Click(object sender, EventArgs e)

{

string value="";

string strSelected="";

for (int i = 0; i < gridView1.RowCount; i++)

{

value = gridView1.GetDataRow(i)["check"].ToString();

if (value == "True")

{

strSelected += gridView1.GetRowCellValue(i, "DISTRICT_NAME");

}

}

MessageBox.Show(strSelected);

}

[c-sharp] view plaincopy

private void buttonX3_Click(object sender, EventArgs e) { string value=""; string strSelected=""; for (int i = 0; i < gridView1.RowCount; i++) { value = gridView1.GetDataRow(i)["check"].ToString(); if (value == "True") { strSelected += gridView1.GetRowCellValue(i, "DISTRICT_NAME"); } } MessageBox.Show(strSelected); }

运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: