views:

781

answers:

2

Can someone describe what the difference between the two are for my project.

Currently I have a List<MyClass> and set the BindingSource to that and a DataGridView to the BindingSource.

I have implemented IEditableObject so when CancelEdit is called I revert my object back to what it was with a Memberwise.Clone()

Will changing my List to a BindingList solve any of this and what are the advantages of using a BindingList?

+1  A: 

A BindingList allows two-way databinding by using events, a List does not fire events when its collection changes.

I don't think it will fix your particular problem.

Gerrie Schenck
+4  A: 

A List<> is simply an automatically resizing array with a couple of helper functions (eg: sort). It's just the data, and you're likely to use it to run operations on a set of objects in your model.

A BindingList<> is a standard wrapper around a list or a collection (it implements IBindingList), that raises events when you modify it. This is required to do two way databinding, since controls need to know when the list changes to update their state.

When you set a BindingSource's DataSource to a List<>, it internally creates a BindingList to wrap your list. You may want to use a BindingList instead if you want to access it outside of the BindingSource, but otherwise it's just the same.

IEditableObject is handled by the BindingSource. It'll call BeginEdit on any implementing object when you change the data in any bound control. You can then call EndEdit/CancelEdit on the BindingSource and it will pass it along to your object. Moving to a different row will call EndEdit as well.

Ilia Jerebtsov
Currently with my List<T> approach calling CancelEdit will not return the item being edited back to its original state hence why I use Clone(). Are you saying a bindinglist will handle that for me?
Jon
No, a BindingList has nothing to do with that functionality. The BindingSource simply calls CancelEdit on the current object regardless of the type of underlying list. There's nothing in the framework that automatically implements object versioning for plain objects. You can use DataTables/DataRows, which keep an original copy of the data for just this purpose.
Ilia Jerebtsov
You say that controls need to know when the list changes, can you explain further? I have a form with a datagridview and then another form from that with populated data. Do I need to concern myself about what your saying in this matter?
Jon
For example, the DataGrid needs to know when items are added to your list in order to add a new row. For that it uses the ListChanged event of the BindingList. If you were binding the grid directly to a List<T>, you would not have the event, and the grid wouldn't be able to know when you changed the list.You don't have to worry about it in your scenario because the BindingSource wraps the List<T> in a BindingList for you. As long as you work with the BindingSource and not the list itself, the controls will stay in sync.
Ilia Jerebtsov