DataGridView CheckBox Column (.NET 2.0/C#)


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();
}
}
}
 
Ad

Advertisements

G

Guest

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();
}
}
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top