当点击查询后,显示信息,勾选复选框,标签显示勾选行的指定列值;
private DataTable GetTable(string sql) //写一个方法用于查询数据库值;
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("数据库连接字符串");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql,con);
sda.Fill(dt);
con.Close();
return dt;
}
添加一列名称为选择,Caption:选择;FieldName为CHECK;ColumnEdit下来列表中选择New,点击CheckEdit后显示的为repositoryItemCheckEdit1;
需要将repositoryItemCheckEdit1的属性NullStyle设置为Unchecked;
private void simpleButton1_Click(object sender, EventArgs e)//查询按钮事件
{
string sql = "查询的语句";
DataTable dt = new DataTable(sql);
dt = GetTable(sql);//调用方法进行查询并存储到dt中
dt.Columns.Add("CHECK", System.Type.GetType("System.Boolean"));//增加一列复选框,类型为Boolean,FiledName为CEHCK;因为查询的时候没有这列,所以需要增加才能勾选
this.gridControl1.DataSource = dt; //设置显示表格的数据源
}
最后设置repositoryItemCheckEdit1的CheckedChanged事件
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
if (gridView1.FocusedRowHandle >= 0)
{
gridView1.CloseEditor();
gridView1.UpdateCurrentRow();
string str2 = "";
for (int i = 0; i < gridView1.RowCount; i++)
{
if (gridView1.GetDataRow(i)["CHECK"].ToString().ToUpper() == "TRUE")
{
str2 += this.gridView1.GetRowCellValue(i, "id").ToString(); //id为编码列值
}
}
this.label2.Text = str2; //用于显示当前选中的列值
}
}