Can a List object be bound to a DataGridView control?

G

Guest

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
 
M

Marc Gravell

If you mean List<T>, then absolutely... just set .DataSource =
theList;

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

Marc
 
G

Guest

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
 
M

Marc Gravell

Design-time binding (in the way you describe) can be done - all you
need to do is select the Type that you are interested in; so, if you
want to display a selection of Order objects in the grid, then select
(via the picker you mention) the Order class. This will generally
setup a BindingSource etc for you. Then, at runtime, you need to tell
it about your orders, so then you would set:

someBindingSource.DataSource = theList;

where theList could be anything suitable - a List<Order>,
BindingList<Order>, etc.

If you do the same entirely at runtime you can avoid having the
BindingSource in the mix, but this can sometimes be useful.

Marc
 
G

Guest

Hello Marc,

Thanks for your answer.

Can a DataGridView that is bound to a class, say class ABC, that has more
data variables that I would like to show on the DataGridView:
For example, ABC has the variables: Var1, Var2, Var3 & Var4. I would like
the DataGridView to have only 2 columns that will be bound to Var2 & Var3 of
class ABC.

Thanks
Eitan
 
G

Guest

Marc,

The last question I asked can be asked also if instead of the class ABC we
would have a List or BindingList ABC?

Thanks
Eitan
 
M

Marc Gravell

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
 
M

Marc Gravell

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<ABC> are 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
 
G

Guest

Hello Marc,

Thanks for your answer.

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

Eitan
 

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