Please see my comment to the question. You have some serious problems with your code you will need to fix.
Now, capturing of the editing events of the editing control embedded in a cell can be difficult, but
DataGridView
provides all hooks for doing so. In your case, the problem can be solved in some simper ways. First of all, you can simply handle the event
System.Windows.Forms.DataGridView.CellEndEdit
:
DataGridView.CellEndEdit Event (System.Windows.Forms).
In the handler method, you can check up the value of the cell:
DataGridViewCell.Value Property (System.Windows.Forms).
You case is very simple: if the editor is the check box, it means that the type of the value is Boolean, so you can bravely type-case the property
DataGridViewCell.Value
value to Boolean and act depending of the value.
If, by some reason, you need to know the combination of the values before and after cell value modification, you can also handle the event
DataGridView.CellBeginEdit
:
DataGridView.CellBeginEdit Event (System.Windows.Forms).
In the simple case of check box cells, you always change from False to True or from True to False, but the user also can leave the value unchanged.
In more complicated cases, you may also need to use the events
DataGridView.EditingControlShowing
,
DataGridView.EditModeChanged
, property
DataGridView.EditingControl
, and so on. Please review all the
DataGridView
members related to editing; they are very well described in MSDN:
DataGridView Class (System.Windows.Forms).
One big warning to everyone: the editing control of the same is reused by
DataGridView
. You have to make sure that you add event handler to the control instance only once. You have to check up that the events handles are already added to the invocation list of the event instances of the same instance of the control and then not add the handlers again. Failure to check it up will result in a memory leak. This is one of the relatively rare cases of the memory leak possibilities in managed memory.
—SA