Search code examples
vb.netwinformsdatagridviewdatasourceformclosing

Detect unsaved changes in DataGridView on form closing VB.NET


Hi I have a DataGridView that has a data table for it's data source. I have a function that uses the DataTable.GetChanges() method and it works beautifully when I run the function while the form is still active; however, I put that function in the form closing event so that it will check for unsaved changes when the form is closed, but for some reason it always comes back with no changes when I run it from the from closing method.

I am guessing that the data source is getting disposed automatically on closing before my function runs to get the changes.

Is there anyway to prevent the data source from getting disposed? I have tried doing e.Cancel=True when in the form closing event and then running my function and it still doesn't work.

Any help with this would be greatly appreciated.


Solution

  • I figured it out. Turns out it only didn't save the change when I typed something into the DataGrid without hitting enter to commit that edit. It actually had nothing to do with the form closing.