What would be the best collection to use when binding a list of data to a DataGridview in C#? I'm currently using just a Generic List but the data grid doesn't update when there is objects added or removed from the list. I've looked at using a BindingList or a ObservableCollection, but can't decide which would be best to use that would update and be easy to sort/filter without having to rebind to the data grid. I'm currently working in windows form on .Net 3.5 framework with plans to move over to WPF soon.
-
I'm also using Linq to do sorting and filtering right now on the generic list of objects. Which I can also do using BindingList
, but doing this forces me to rebind the list to the data grid. – Nick O Nov 12 '09 at 15:56
7 Answers
ObservableCollection<T>
won't work for aDataGridView
: it implementsINotifyCollectionChanged
, notIBindingList
, and theDataGridView
doesn't know aboutINotifyCollectionChanged
. It is intended for WPF bindings and is not used in Windows FormsBindingList<T>
is a good option, but note that it doesn't support sorting or filtering out of the box. However, you can find some custom implementations of these features on the web.DataTable
is probably your best option if you need sorting and/or filtering capability

- 286,951
- 70
- 623
- 758
-
-
-
I have a 3 tier application with a business object layer and I need collection of one type of business object. Is that possible to do with a DataTable? I thought a DataTable could only be use with data strait from the database. – Nick O Nov 19 '09 at 15:14
-
It can be used for any data, not just for data from database (although it is its primary goal). However the columns can only contain primitive types, so you would have to copy your business object's properties to the datatable – Thomas Levesque Nov 19 '09 at 16:31
The data binding framework is completely different between WinForms and WPF, so (in general), there isn't a "best choice" for the both of them.
For WinForms, using the generic BindingList<T>
will accomplish most of what you want (though it doesn't handle changes to individual items; you'll have to implement that yourself).
For WPF, ObservableCollection<T>
serves a similar purpose.

- 182,639
- 35
- 285
- 343
-
-
`BindingList
` uses the `IBindingList` interface, which is what WinForms uses for its list data binding. WPF uses `INotifyCollectionChanged`. – Adam Robinson Nov 12 '09 at 16:39
Actually Microsoft reccomends using a Collection as your binding collection rather than a List because of the ability to do automatic functions like when adding and removing items, clearing the collection, or setting the value of an existing item.
Collection Class on MSDN.

- 6,272
- 9
- 35
- 57
A DataTable, perhaps?
Also, you can often force the gridview to repaint and include the new items by calling DataGridview.Invalidate() immediately after items are added.

- 72,686
- 18
- 132
- 173
I do not think there's a hard/general rule of what type of collection is best suited for DataGridView.
It really depends on a several factors:
- The nature of the data
- What are the operations (if any) to be performed from the UI to the DB (e.g. CRUD, sort, filter)
- Size of the data
etc etc..

- 25,490
- 6
- 66
- 63
I created my own collection inheriting from BindingList which supports sorting, filtering, etc. It works well now but was a lot of work, I don't really recommend doing one yourself... I think there's one already implemented on CodeProject or a similar site, I'll give you the link if I find it.
Edit:
I found this CodeProject article with an implementation of IBindingList which supports sorting, filtering, etc. I didn't test it though, so I don't know how good the implementation is, but it might be worth checking it out.

- 17,626
- 1
- 48
- 58