[英]How to check a CheckBox in DataGridview that whether it is checked or not
我在Windows窗体的DataGridView中有13个CheckBox,并且我想在选中第一个CheckBox时检查所有CheckBox,并在取消选中第一个复选框时取消选中所有Checkbox,所以我该怎么做。 我的代码可用于检查所有复选框,但在取消选中时会失败。 我正在使用CellContentClick事件。 这是我的代码
if (e.ColumnIndex == 1)
{
for (int k = 2; k <= 13; k++)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[k];
DataGridViewCheckBoxCell checkCell = cell as DataGridViewCheckBoxCell;
checkCell.Value = true;
}
}
尝试这个:
if (e.ColumnIndex == 1)
{
DataGridViewCheckBoxCell firstCell =
dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewCheckBoxCell;
if(firstCell == null)
{
return;
}
for (int k = 2; k <= 13; k++)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[k];
DataGridViewCheckBoxCell checkCell = cell as DataGridViewCheckBoxCell;
checkCell.Value = firstCell.Value;
}
}
您使用for循环仅分配true
值,还需要将false
值分配给复选框,请取消选中所有复选框。
if (e.ColumnIndex == 1)
{
for (int k = 2; k <= 13; k++)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[k];
DataGridViewCheckBoxCell checkCell = cell as DataGridViewCheckBoxCell;
checkCell.Value = dataGridView1.Rows[1].Cells[1].Value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.