0

I have a WinForm with a DataGridView control. It contains a CheckBox column.

I use a CellMouseClick event with evaluation of the current column index for the evaluation. Another form opens after clicking on the check box regardless of the logical state of the check box.

Depending on the outcome of the input in the new form, I try to set the State of the check box.

Unfortunately, my setting is overridden by the default behavior of the check box.

I.e. if the check box was set, finally it is unset and vice versa.

My question is, if there is a possibility to suppress or work around this behavior. Possibly something like e.Handled ?

I have also used the CellContentClick event but the result is the same.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
H.Pradella
  • 13
  • 4

2 Answers2

0

Use the event DataGridView.CellValidating. In the link there is an example how to cancel the event of the current cell. When there is a checkbox, this event is raised only when the user leaves the cell. To solve this problem use the event DataGridView.CurrentCellDirtyStateChanged Event and force the commit to change. This is an example.

Community
  • 1
  • 1
ZanAl
  • 73
  • 1
  • 3
0

Thanks very much for your advice. Unfortunately using the way you described I can not achieve the desired effect.I use the Events CurrentCellDirtyStateChanged and CellValidating as suggested. The behavior is something strange in my eyes. Inside the CurrentCellDirtyStateChanged Event I checked the CellValue of the Checkbox before (it is true) and after (it is false) calling CommitEdit. The CellValidating Event is not raised. Cancelling the Event is not possible.

Ok, when leaving the Form - that has been opened by a click on the CheckBox - I did not leave the CheckBox cell.

So I tried to solve the problem by leaving the CheckBox cell by setting the CurrentCell to another cell. As a result the CellValidating Event is raised. But setting e.Cancel = true results in an Exception.

After some additional trying based on your suggestions I can achieve the desired effect without using the CurrentCellDirtyStateChanged and CellValidating Events. After leaving the additional Form now I get the return value from the Form, set the CheckBox cell according to this value, set the CurrentCell to the next cell in the CurrentRow and call RefreshEdit. The CheckBox state is always set to the correct value and is not changed if I leave the additional Form without changes. That's what I want. Perhaps this may help others who have similar problems.

H.Pradella
  • 13
  • 4