views:

49

answers:

2

Hi,

I have a form with a DataGridView on it.

In this DataGridView there is a DataGridViewComboBoxColumn. When changing the value of a cell of this DataGridViewComboBoxColumn, the CellValueChanged-event is only fired when leaving the cell (for example, if I click into another cell). The event is not fired when I change the value and then just close the form.

So, how can I save the changes (if there are any changes), if my form is simply closed?

[UPDATE]

The CellValueChanged is not fired when the form the DataGridView is on is shown through form.ShowDialog():

using (FormWithDataGridView form = new FormWithDataGridView()) {
    form.ShowDialog();   // DataGridView on form fires no CellValueChanged-Event when form gets closed
}
A: 

Hi, have you tried using dataGridView1.CellParsing? This should (as I understand it) trigger even if the focus of the cell is lost due to closing the containing Form. This will only trigger if the user has changed the value of the cell.

[Edit] Now when I think about it a bit more I would try dataGridView1.CellEndEdit if I was you. That is better..[/Edit]

Mikael
+1  A: 

From the community content post on the MSDN entry for ShowDialog, when you close a modal form, it is just being hidden so the calling code can still have access to the DialogResult or other properties of the form. Apparently, this is why the CellValueChanged event on the DataGridView is not firing (CellParsing and CellEndEdit events also do not get raised).

As a workaround, in the form closing event, you can remove focus from the DataGridView, which will cause the CellValueChanged event to fire if necessary. If you don't have any other controls on the form to set focus to, you can put a label on the form and give it focus.

adrift