Binding a IEnumerable list to a DataGridView?

J

Jeff

..NET 2.0
VS2005

I'm wondering if it possible to bind an IEnumerable list directly to a
DataGridView? if it is possible then I'm wondering how it should be done?

I ask because I think it must be better to use that, than the implementing a
foreach loop adding a row to the DataGridView for every element in the
IEnumerable list...

Any suggestions?

Jeff
 
M

Marc Gravell

You can bind an IList to a DataGridView... full data-binding often
requires random-access. As such, one option is to use
List<SomeType> list = new List<SomeType>(myIEnumerableList);
dataGridView1.DataSource = list;

or alternatively use BindingList<SomeType> if you want notifications
etc

Marc
 
S

Sheng Jiang[MVP]

You can use a BindingSource object as the datasource of the grid, and set
the datasource of the bindingsource object to a type. At runtime you can
assign the datasource property of the bindingsource object to a collection
of that type, such as a ReadOnlyCollection or a ObservableCollection object
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top