Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Can a List object be bound to a DataGridView control?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 26 Jun 2007 21:58 GMT
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
Marc Gravell - 26 Jun 2007 22:04 GMT
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
Eitan - 26 Jun 2007 22:42 GMT
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

> If you mean List<T>, then absolutely... just set .DataSource =
> theList;
[quoted text clipped - 5 lines]
>
> Marc
Marc Gravell - 27 Jun 2007 05:01 GMT
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
Eitan - 27 Jun 2007 05:18 GMT
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

> 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
[quoted text clipped - 12 lines]
>
> Marc
Marc Gravell - 27 Jun 2007 10:37 GMT
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
Eitan - 27 Jun 2007 05:22 GMT
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

> 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
[quoted text clipped - 12 lines]
>
> Marc
Marc Gravell - 27 Jun 2007 10:40 GMT
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
Eitan - 27 Jun 2007 14:24 GMT
Hello Marc,

Thanks for your answer.

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

Eitan

> Please clarify what you mean...
>
[quoted text clipped - 12 lines]
>
> Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.