开发者

How to get cell value change when closing form?

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 show开发者_StackOverflown through form.ShowDialog():

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


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.


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]


Ran into a similar situation myself, but with the last checkbox updated programmatically--whouldn't update unless I switched rows.

Try calling the BindingSource.EndEdit method just before you update the combobox.

For me I call the data adapter afterwards and it recognizes a change to the datagridview--so it does the work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜