下面是设置CheckBox是选中(1)还是没有选中(null):
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (e.ColumnIndex == 1)
{
if (((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag == null)
((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
else
{
((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "0";
}
}
}
下面是遍历CheckBox:
for (int j = 0; j < dataGridView2.Rows[i].Cells.Count; j++)
{
//判断是否是checkbox列
if (this.dataGridView2.Columns[j].GetType().Name == "DataGridViewCheckBoxColumn")
{
if ((((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag == null) || (((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag.ToString() == "0"))
{
strTmp1 = "";
}
else if (((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag.ToString() == "1")
{
ckb = dataGridView2.Rows[i].Cells[j].Value.ToString();
strTmp1 = strTmp1 + "," + ckb;
}
break;
}
}