I have 13 CheckBoxes in a DataGridView in windows form and I want to check all CheckBoxes when the first CheckBox is checked and uncheck all the Checkboxes when the first one is unchecked so how would i do it. my code works for checking all checkboxes but it fails while unchecking. i am using CellContentClick event. here is my code
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;
}
}
Try this:
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;
}
}
you assign only true
value using for loop you also need to assign false
value to checkbox uncheck your all checkbox..
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.