I’m curious about the differences between ObservableCollection and BindingList because I’ve used both to notice for any additions or deletions in Source, but I’m not sure when to use one over the other.
Why would I pick one of the options above over the other?
ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();
BindingList<Employee> lstEmp = new BindingList<Employee>();
Asked by Azhar
Like any other collection, an ObservableCollection can be updated through the user interface. The true distinction is straightforward:
INotifyCollectionChanged is implemented by ObservableCollectionT>, which offers notification when the collection is modified (as you would have predicted). When the ObservableCollection is updated, the binding engine can update the UI.
BindingListT>, on the other hand, implements IBindingList.
IBindingList notifies you when a collection changes, but it does more. It provides a lot of features that the UI can leverage to provide a lot more than just UI updates in response to changes, such as:
ObservableCollectionT> lacks all of these features.
Another difference is that when items implement INotifyPropertyChanged, BindingList relays item change notifications. The BindingList receives a PropertyChanged event from an item and raises a ListChangedEvent with ListChangedType. ItemChanged and OldIndex=NewIndex (OldIndex=-1 if an item was replaced). Item alerts are not relayed by ObservableCollection.
BindingList is not an option in Silverlight; however, you can use ObservableCollections and ICollectionView instead (and IPagedCollectionView if I remember well).
Answered by Eilistraee
BindingList is for WinForms, while ObservableCollection is for Windows Presentation Foundation.
BindingList isn’t well-supported in WPF, and you’d only use it in a pinch.
Answered by Dean Chalk
The accepted answer has addressed the most relevant distinctions, such as features and change notifications about the enclosed items, but there are more worth mentioning:
BindingListT> uses an IndexOf lookup to find the added item when AddNew is called. If T implements INotifyPropertyChanged, IndexOf searches the index of a modified element as well (though there is no new lookup as long as the same item changes repeatedly). If the collection has thousands of elements, ObservableCollectionT> (or a bespoke IBindingList implementation with O(1) lookup cost) may be a better choice.
Copy vs. wrapping
Both ObservableCollectionT> and BindingListT> contain a constructor that takes an existing list as an argument. When they are instantiated by another collection, they act differently:
Answered by György Kőszeg
Another significant difference between ObservableCollection and BindingList that is useful and could influence your decision on the matter is:
Change Handler for BindingList:
ObservableCollection Collection change:
The preceding conclusion applies to model classes that implement INotifyPropertyChanged. When a property in an item is updated, none raises the changed event by default.
Answered by Kylo Ren
Both offer benefits and cons that must be discovered over time.
Because the change notification event happens immediately after an item has been removed and simply offers an index, I experienced some issues with BindingList (which means that you have to keep track of which object was at which location if you have implement some post-removal mechanism). ObservableCollection, on the other hand, returns a list of the things that have been removed.
The AddNew() function of BindingList allows a derived class to implement a factory pattern, such as initializing new items with values from the parent collection (for instance, a foreign key to the parent if the collection contains child items).
from an ObservableCollection using Entity Framework’s (ToBindingList extension), and that the resulting (derived) BindingList has capabilities like sorting that the plain vanilla one lacks.
Answered by Jean-Xavier Bardant
Post is based on https://stackoverflow.com/questions/4284663/difference-between-observablecollection-and-bindinglist