这是场景。
我有checkbox
(名称:“Check All” ID:chkItems)和datagridview
。当我单击此复选框时,datagridview
上的所有复选框也将被选中。
我还在网格上添加了复选框列。
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
GridView1.Columns.Add(CheckboxColumn);
这是复选框的隐藏代码。 row.Cell
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in GridView1.Rows)
{
DataGridViewCheckBoxCell chk = e.row.Cells(0);
if (chk.Selected == false)
{
row.Cells(0).Value = true;
}
}
}
最佳答案
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
代替
DataGridViewCheckBoxCell chk = e.row.Cell(0);
*编辑:*我认为你真的想这样做:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}
关于c# - 检查 DataGridView 上的所有复选框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13242861/