473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
5 6632
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Martin Widmer | last post by:
Hello again! I have a datagridview control on my form and am using VS.Net 2005. One column is set up as combo box column, and when I try to set the datasource for that combobox column at design...
2
by: Nathan | last post by:
Hi, I am trying to bind a DataGridView to a generic List<T>. I've set up the columns in the designer, then used this code: dgv.DataSource = MyList; dgv.Columns.DataPropertyName = "Property1"...
3
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my...
0
by: polaris431 | last post by:
I would like to have a combobox control that when clicked, displays a DataGridView in-place of the normal dropdown list. I want it to be a DataGridView with editing capabilities. The DataGridView...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
0
by: Microsoft | last post by:
Using .Net 2.0. I have a List of objects bound to a ObjectDataSource which is connected to a DataGridView. Everything displays properly but I am having a problem retriving a selected object from...
4
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a DataGridView bound to a collection of Custom Objects. I have enabled adding new rows, but since my object does not have a public constructor (it uses a factory method), I get an error...
7
by: Ciri | last post by:
Hi all, I've a problem with a datagridview. The data source of the control is a List<t> where t is type of a class I've created. The class (Person) has some properties like Name, Surname and so on....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.