views:

320

answers:

2

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?"

I can't work out how to detect this on the binding source

Any ideas?

Thanks

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

A: 

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

Matt Jacobsen
The bindingsource datasource is a List<T>
Jon
You can't watch collections. An alternative would be a bunch of TextChanged events that update a private "Changed" variable that you check before saving
Matt Jacobsen
I had that thought but its nasty
Jon
It is! Is using a DataSet/DataTable out of the question?
Matt Jacobsen
I think so because the List contains List<MyClass> which has all the properties etc of that item being edited/added
Jon
You could still do it: those properties in MyClass just need to be mapped to DataColumns in your new DataTable. Depends on your situation how icky this is though.
Matt Jacobsen
Or you can monitor the changes in MyClass. That's a little prettier than a bunch of TextChanged events in teh form.
Matt Jacobsen
How could I monitor my class as its properties wont get changed until you call the MyClass.Save. I thought the databinding would provide me with the monitoring but it isnt
Jon
A: 

From my updated question I found I had to store a current version of the object at BeginEdit using Memberwise.Clone and then in CancelEdit I restored that to the current.

Jon