Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect DataGridView CheckBox event change?

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either

  1. Triggers as soon as the CheckBox is clicked but before its checked state changes, or
  2. Triggers only once the CheckBox looses its focus

I can't seem to find event that triggers immediately after the checked state changes.


Edit:

What I am trying to achieve is that when the checked state of a CheckBox in one DataGridView changes, the data in two other DataGridViews changes. Yet all the events I have used, the data in the other grids only changes after the CheckBox in the first DataGridView looses focus.

like image 685
PJW Avatar asked Aug 07 '12 09:08

PJW


People also ask

Which event is triggered when the user clicks in a check box C#?

Whenever a user clicks a Windows Forms CheckBox control, the Click event occurs.


2 Answers

I found @Killercam's solution to work but was a bit dodgy if the user double clicked too fast. Not sure if other's found that the case either. I found a another solution here.

It uses the datagrid's CellValueChanged and CellMouseUp. Changhong explains that

"The reason for that is OnCellvalueChanged event won’t fire until the DataGridView thinks you have completed editing. This makes senses for a TextBox Column, as OnCellvalueChanged wouldn’t [bother] to fire for each key strike, but it doesn’t [make sense] for a CheckBox."

Here it is in action from his example:

private void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e) {     if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)     {         // Handle checkbox state change here     } } 

And the code to tell the checkbox it is done editing when it is clicked, instead of waiting till the user leaves the field:

private void myDataGrid_OnCellMouseUp(object sender,DataGridViewCellMouseEventArgs e) {     // End of edition on each click on column of checkbox     if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)     {         myDataGrid.EndEdit();     } } 

Edit: A DoubleClick event is treated separate from a MouseUp event. If a DoubleClick event is detected, the application will ignore the first MouseUp event entirely. This logic needs to be added to the CellDoubleClick event in addition to the MouseUp event:

private void myDataGrid_OnCellDoubleClick(object sender,DataGridViewCellEventArgs e) {     // End of edition on each click on column of checkbox     if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)     {         myDataGrid.EndEdit();     } } 
like image 34
jsturtevant Avatar answered Oct 04 '22 15:10

jsturtevant


To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...

private void dataGridViewSites_CellContentClick(object sender,      DataGridViewCellEventArgs e) {     dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit); }  /// <summary> /// Works with the above. /// </summary> private void dataGridViewSites_CellValueChanged(object sender,      DataGridViewCellEventArgs e) {     UpdateDataGridViewSite(); } 

I hope this helps.

P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx

like image 124
MoonKnight Avatar answered Oct 04 '22 14:10

MoonKnight