Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if user has modified data using bindingsource?

I have a DataGridView bound to a bindingsource which is bound to a List<T>. The user clicks a row that goes to a form with textboxes, etc. The textboxes are databound like so:

if (txtID.DataBindings.Count == 0)
    txtID.DataBindings.Add("Text", bindingSource, "Title");

I want to be able to detect if the user has modified any data in the controls when they click the close button, so I can prompt them to say "You have un-saved work. Do you want to Save?"

How do I detect this on the binding source?

UPDATE: I have worked out that I can do bindingSource.EndEdit() which pushes the changes to my item in the list. In my item, I can then say if Dirty throw a Messagebox but if they click "No" to saving the information, the CancelEdit does not work.

like image 351
Jon Avatar asked Feb 05 '10 09:02

Jon


2 Answers

If your object within the List support the INotifyPropertyChanged event and you replace the List<T> by a BindingList<T> you can subscribe to the ListChanged event of the BindingList to get informed about any changes made by the user.

like image 133
Oliver Avatar answered Nov 28 '22 19:11

Oliver


If you're bound to a DataSet then you're in luck: it has a HasChanges Property. You can get the actual changes by calling GetChanges on the dataset. This returns a new dataset containing a copy of all changed rows

like image 35
Matt Jacobsen Avatar answered Nov 28 '22 20:11

Matt Jacobsen