Commit current dirty cell on clicking save button from any datagridview on form?

Hook up all your datagridviews to the same Leave event handler. The leave event is processed before the click event. Inside your leave event, capture the datagridview that just lost focus.

When the save button is clicked, check if the last datagrid view that was left has any unsaved data.

Hook up all your datagridviews to the same Leave event handler. The leave event is processed before the click event. Inside your leave event, capture the datagridview that just lost focus.

When the save button is clicked, check if the last datagrid view that was left has any unsaved data... Example: DataGridView _lastDataGridView = null; private void dataGridView_Leave(object sender, EventArgs e) { _lastDataGridView = sender as DataGridView; } private void saveButton_Click(object sender, EventArgs e) { if (_lastDataGridView! = null) { // check if data needs saving... } } Edit 1: As for you not receiving the leave event before the click event, I'm not seeing that behavior. To reproduce what I've done, create a form with a DataGridView and a button.

Add two text box columns to the DataGridView, hook up the events as described and see if the _lastDataGridView member is set when executing sendButton_Click. It is on my end. Edit 2: After trying my datagridview I noticed that the data was always being saved.So I suspected you had some different settings.

I turned on "VirtualMode. " This causes the same behavior that you're describing. If possible, try turning VirtualMode off and see if the data gets saved to the DataGridView as expected.

Otherwise try implementing the suggestions outlined in this MSDN article.

Thanks, Jason. I will check this now. The button in question is on a toolstrip but perhaps that will not matter.

Your suggestion is clear, logical and helpful and should be very easy to test. – Charles Hankey Dec 15 '09 at 3:04 Let me know how it turns out. In my experience I've always received the leave event before any other event (due to a mouse click) occurs.. .

However, I can see one scenario where the toolbar button handler may kick in without losing focus. If that happens, use the Enter event instead, to track which datagridview has focus... – Jason D Dec 15 '09 at 17:59 the one scenario is if you've hooked up the save command to accelerator keys, such as "Ctrl-S" the focus will still be on the datagridview.... – Jason D Dec 15 '09 at 18:00 You were all quite right, of course, about the leave event firing when you said - the problem is it turns out that by the time the leave event fires the cell has already reverted to its original value and is no long dirty. I have trapped the currentcelldirtystatechanged event as suggesting in the other thread, check for iscurrentcelldirty and then force the commitedit.

Of course this fires on each keystroke as the cell is no longer dirty after the commit then becomes dirty again with teh next character. Completely blows up on datetime bound textbox, of course, as the first char is invalid – Charles Hankey Dec 15 '09 at 20:49 Formatting for currency etc doesn't seem to create a problem. I've bypassed the routine for currentcell.

Valuetype is gettype(datetime) and the other values work fine. – Charles Hankey Dec 15 '09 at 20:51.

Assuming you are using SQL, you would create an UpdateCommand, SelectCommand, and DeleteCommand for each DGV. When you want to "save" the changes, just call DataAdapter. Update(myDataSet, "TABLE NAME"); for the DataAdapter associated with your DataGridView.

I use this technique now for one DGV and it works fine.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions