I am using Binding List in my application along with ItemChanged event.
Is there any way I could know the previous values of properties in ItemChanged event. Currently I am adding a separate property named 'OldValue' to achieve this.
Is there any way to know about the deleted item in item changed event. I am not able to find any way to know which item has been deleted from the list.
Actually, deletion happens before the event fires. So, you cannot get to the item being removed. You definitely need some additional logic for that You can, however, inherit from BindingList, and override RemoveItem:
Replicate the BindingList constructors. Don't make it cancellable to avoid misconceptions. You may also find some help here: http://connect.microsoft.com/VisualStudio/feedback/details/148506/listchangedtype-itemdeleted-is-useless-because-listchangedeventargs-newindex-is-already-gone
Hope this helps.
If I understand correctly, you want to get info about item which was deleted from binding list.
I think the easiest way to do this will be creating your own binding list which derives from binding list.
Inside you'll have RemoveItem method overriden, so BEFORE removing item from binding list, you'll be able to fire event containing item which is going to be removed.
For the sake of example I created type myInt implementing INotifyPropertyChanged - interface is just to make dataGridView refresh after adding/deleting elements from binding list.
I'm initializing binding list with ints (myInts to be precise), then I'm binding list to dataGridView (for presentation purpose) and subscribing to my BeforeRemove event.
If BeforeRemove event was raised I have item which was deleted
Below is whole example code (drop 3 buttons and dataGridView on form) - button 1 initializes binding list, button 2 adds item to list, button 3 removes item from biding list
ASNWER TO COMMENT
"The other part of the question is => Is there any way to know the old value of the item which is changed in the list? In ListChangedEvent does not share anything"
To see old value of item you can override SetItem method
It fires when object at specified index is changed, like this
The tricky part is, if you try to modify item's property directly
SetItem won't fire, it has to be a whole object replaced.
So we will need another delegate & event to handle this
Then we can subscribe to this
and handle it
Updated code (4 buttons required, 4-th modifies selected item)
An alternative approach to this problem is to wrap an ObservableCollection with a BindingList. This code works for me -
In the specific case you're using this
BindingList
with aDataGridView
, you can use theUserDeletingRow
event from the datagrid, where:This is a very old 8 years issue that Microsoft doesn't want to fix (I guess for regression risk reason). Here is the connect link to it:ListChangedType.ItemDeleted is useless because ListChangedEventArgs.NewIndex is already gone
There are various workaround proposed. The last one by If-Zen (2013/12/28) seems pretty decent, I'll quote it here with a slightly modified version: