DataGridView CheckBox Column (.NET 2.0/C#)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have encountered the following issue :

I have a DataGridView control on a windows form with a checkbox column. When
the user clicks the column header, I need to toggle the selected state of the
checkboxes...basically a 'select/deselect all'.

Everything is working fine, except the checkbox in the very first row does
not show the check mark unless i actually click the control itself. rows 1
thru n work fine. I have verified that the value is being set on the first
row's checkbox, so it seems to be a display issue. Here is the code:

void grdDirectReports_ColumnHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{

DataGridView dgv = (DataGridView)sender;

if (e.ColumnIndex == dgv.Columns["Selected"].Index)
{
if (e.RowIndex < 0)
{
for (int i = 1; i < dgv.Rows.Count; i++)
{
dgv.Rows.Cells[0].Value =
!lastDRGridCheckAllSelected;
}
lastDRGridCheckAllSelected = !lastDRGridCheckAllSelected;

dgv.Refresh();
}
}
}
 
You are starting at row 1 instead of row 0 in your for loop, try this:
void grdDirectReports_ColumnHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{

DataGridView dgv = (DataGridView)sender;

if (e.ColumnIndex == dgv.Columns["Selected"].Index)
{
if (e.RowIndex < 0)
{
for (int i = 0; i < dgv.Rows.Count; i++)
{
dgv.Rows.Cells[0].Value =
!lastDRGridCheckAllSelected;
}
lastDRGridCheckAllSelected = !lastDRGridCheckAllSelected;

dgv.Refresh();
}
}
}
 
Back
Top