0

I've implemented a GenericCollection using IBindingList, and it works great and fires events for when items are added or removed. It doesn't fire events when items are changed/edited as expected. Is there a simple way to implement logic to catch a change or edit without having to implement INotifyPropertyChanged within each class that uses this collection?

I might be looking for something not possible, but I'm basically looking to mimic functionality of a DataTable without having to code in the INotifyPropertyChanged for each class that uses the collection and I want to have my grid receive the edits/changes.

Thanks,

Mark

mservidio
  • 12,817
  • 9
  • 58
  • 84
  • It sounds like you've just re-invented either `Collection` or `ObservableCollection` from `System.Collections.ObjectModel`. – SLaks Aug 26 '11 at 16:12

2 Answers2

2

You should use the BindingList<T> class, which already does all that.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Good point. I had thought that would work, and I tried that initially. I'm using Telerik components, and I'm trying to have my RadGridView reflect any changes made to the collection. However, edits aren't updating in the grid. From this link it appears that BindingList should work though. http://www.telerik.com/help/winforms/gridview-populating-with-data-binding-to-bindinglist.html – mservidio Aug 26 '11 at 16:24
  • Oh, I missed a key peice of info from that site: By default grid is only notified when items are inserted or deleted. To notify the grid when an item property is being changed, your custom class should implement the INotifyPropertyChanged interface. The example below creates a custom object "MyObject" that implements the INotifyPropertyChanged interface. Notification has been enabled for the MyString property only. – mservidio Aug 26 '11 at 16:25
  • But BindingList doesn't fire events when an item is edited/changed, does it? From reading on msdn, looks like it just notifies on new/removed items. I'm looking to see if there's a generic way to identify list item edits without having to implement INotifyPropertyChanged... – mservidio Aug 26 '11 at 16:27
  • @mservidio: You must implement `INotifyPropertyChanged`. You cannot magically find out when a property changes without raising an event. Once you implement `INotifyPropertyChanged`, the `BindingList` will forward the event to the collection. – SLaks Aug 26 '11 at 17:50
  • 1
    Use http://visualstudiogallery.msdn.microsoft.com/bd351303-db8c-4771-9b22-5e51524fccd3 to help. – SLaks Aug 26 '11 at 17:51
1

Use System.Collections.ObjectModel.ObservableCollection class.

Ruberoid
  • 1,585
  • 1
  • 9
  • 12