467,119 Members | 1,015 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,119 developers. It's quick & easy.

Can a List object be bound to a DataGridView control?

Hello,

I am writing an application in c#.

I have a List object that I would like to bind it to a DataGridView control.
Can I do that, and if so how?

If I can not do that, is there any other way that would bind a List (or a
similar object) to the DataGridView control?

Thanks
Eitan

Jun 26 '07 #1
  • viewed: 6347
Share:
5 Replies
If you mean List<T>, then absolutely... just set .DataSource =
theList;

However! If you want proper change notifications, then note that
BindingList<Tis a better option - same thing, but the grid will show
updates made independently (assuminng your object model itself
supports notifications).

Marc

Jun 26 '07 #2
Thanks for your answer.

I added BindingSource control to my Form. In the BindingSource control, the
DataSource only let me select an object such as the object of the Form and
not the BindingList I declared.

In the "Data Source Configuration Wizard" I selected an Object, the drop
down list of the "Select the Object You Wish to Bind to" only shows the
assemblies not the BindingList...

Thanks
Eitan

"Marc Gravell" wrote:
If you mean List<T>, then absolutely... just set .DataSource =
theList;

However! If you want proper change notifications, then note that
BindingList<Tis a better option - same thing, but the grid will show
updates made independently (assuminng your object model itself
supports notifications).

Marc

Jun 26 '07 #3
Yes; if using auto-generated columns, simply remove the ones you don't
need (you may need to turn off AutoGenerateColumns [or whatever]
afterwards).

If adding columns manually, simply don't add the ones you don't need.

Marc
Jun 27 '07 #4
Please clarify what you mean...

You can't really use a single instance of ABC as the DataSource [with
a grid] unless this is a collection itself (IList/IListSource) - but I
don't think this is what you were suggesting.

So you need to use *some* form of collection of ABCs - and List<ABC>
or BindingList<ABCare common choices. But others would work.

However, at design time you must bind to the Type of ABC - you will
often see this in the Designer.cs as
"yourBindingSource.DataSource=typeof(YourNamespace .ABC);". This is a
feature of the IDE. If you don't use the IDE for binding you can omit
this step and bind just to the data.

Marc
Jun 27 '07 #5
Hello Marc,

Thanks for your answer.

I do have a BindingList of this ABC and that is what I meant.

Eitan

"Marc Gravell" wrote:
Please clarify what you mean...

You can't really use a single instance of ABC as the DataSource [with
a grid] unless this is a collection itself (IList/IListSource) - but I
don't think this is what you were suggesting.

So you need to use *some* form of collection of ABCs - and List<ABC>
or BindingList<ABCare common choices. But others would work.

However, at design time you must bind to the Type of ABC - you will
often see this in the Designer.cs as
"yourBindingSource.DataSource=typeof(YourNamespace .ABC);". This is a
feature of the IDE. If you don't use the IDE for binding you can omit
this step and bind just to the data.

Marc
Jun 27 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nathan | last post: by
reply views Thread by Microsoft | last post: by
4 posts views Thread by =?Utf-8?B?VGVycnk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.