Question

I'm writing a tabbed WinForms application that contains multiple DataGridView controls which are bound to BindingSources. The BindingSources, in turn, are bound to BindingLists of business objects. My business objects implement INotifyPropertyChanged.

My TabControl has three TabPages (A, B and C). If my user has made changes to the data in the DataGridView control on tab A and then tries to switch to tab B or C without committing the changes (in this case, writing the data to disk), I need to prompt them to save the changes or lose them.

In order to determine if changes have been made on a given tab, I created "pendingChange" variables for each tab. Each DataGridView control has it's own CellValidating event handler where I validate the data as the user makes changes. At the end of that event handler, if the change has passed all of the validation checks, I set the pendingChange variable for that tab to true. When my user clicks the button to write the changes back to disk, I reset the pendingChange variable. So anytime my user attempts to change to a different tab, I check this variable to determine if there are any changes that need to be saved first.

Now I've realized that a big flaw with this design is that the CellValidating event for the DataGridView control is fired every time a cell loses focus. So now I need to find a different way to track when a cell value has been changed. Is there another event that the DataGridView control exposes that would be better suited to this purpose? Or perhaps there's an event for the BindingList that would help me achieve this functionality? If I have to, I can add a boolean "modified" property to my business object and update that as required. If it's possible to avoid that, I would like to because one of the BindingLists contains 150,000 objects at any given time and iterating through that might not be terribly fast.

Was it helpful?

Solution

BindingSource has Events. E.g. CurrentChanged event occurs when the currently bound item changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top