I created a DataGridView control where the first column is a checkbox. When I click inside the check box, the code correctly executes. HOWEVER, when the click occurs in the cell but not in the checkbox, the code correctly handles the state of the checkbox but does not update the checkbox itself, so it remains in the state it was before the click.
How do I correct this?
private void myDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1) return; //check if row index is not selected
DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
ch1 = (DataGridViewCheckBoxCell)myDataGrid.Rows[e.RowIndex].Cells[0];
if (ch1.Value == null)
ch1.Value = false;
switch (ch1.Value.ToString())
{
case "False":
ch1.Value = true;
break;
case "True":
ch1.Value = false;
break;
}
//BUT doesn't seem to matter what I do with ch1.Value. Inside the checkbox all is OK but
//outside, no.
}
If I click in another cell on the row, the check box is handled correctly. ONLY when I click in the check box cell but NOT in the checkbox itself.
What I mean is (and apologies), the code correctly executes but the UI does not update correctly to reflect the change. So, if the checkbox is unchecked before it appears to unchecked afterwards as well, even though it is actually checked.
Thanks RON
I'm not 100% sure what causing this behavior, but I believe it has to do with the check box cell entering edit mode the moment you click inside of it.Confirmed, this issue is definitely caused by entering edit mode the moment you click inside the cell. See edit at the end of the post.Because you are setting the value of the cell yourself, you can get around this issue by just making the check-box column readonly as well.
The following code worked fine for me in using LINQPad (I look liberties to simplify the handler logic):
The only oddity I noticed is the second click in a double-click doesn't get routed to the
CellClick
event handler. If you really want this behavior you can tie the same handler to theCellDoubleClick
event.Edit:
If you don't like making the first column read-only, then you can simply add a call to
EndEdit
after you change the value:This was hinted at by a comment on another question by user NeverHopeless.