How to tell if user has modified data using bindingsource?
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.
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
After trying different thing I ended up with this piece of code:
private MyClass currentItem = null;
private bool itemDirty = false; // can be used for "do you want to save?"
private void bindingSource_CurrentChanged(object sender, EventArgs e)
{
var handler = new PropertyChangedEventHandler((s, e2) => itemDirty = true);
var crnt = currentItem as INotifyPropertyChanged;
if(crnt != null) crnt.PropertyChanged -= handler;
currentItem = (MyClass)bindingSource.Current;
crnt = currentItem as INotifyPropertyChanged;
if(crnt != null) crnt.PropertyChanged += handler;
itemDirty = false;
}
It works fine for me, although I save lots of state information in the Windows Form's instance fields. However, twiddling with CurrentChanged
and CurrentItemChanged
did not help me.