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# / February 2008

Tip: Looking for answers? Try searching our database.

C# DatagridView with List<t> as DataSource

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ciro - 07 Feb 2008 14:02 GMT
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.
Now my datagridview is bounded to a collection of Person:

dgwPersons=new DataGridView();
dgwPersons.AutoGenerateColumns=false;
dgwPersons.Columns.Add("Name","Name");
dgwPersons.Columns[0].DataPropertyName="Name";
dgwPersons.Columns.Add("Surname","Surname");
dgwPersons.Columns[1].DataPropertyName="Surname";
BindingSource personsBindingSource=new BindingSource();
personsBindingSource.DataSource=Persons; //Persons is the collection
dgwPersons.DataSource=personBindingSource;

Most of properties of the Person class are strings (like Name and Surname),
so I had no problem to bound them to the dgw.
I can even edit the fields in the dgw and have the corresponding property in
memory changed.

Now the question is that the class Person have a property (job) of the type
of Job (another class);
Furthermore I've got another list<t> where t is type of Job.
The collection contains all possible jobs.
I'd like to have a combobox column in my dgw that shows the corrent job of
the person and gives the user the chance to choiche between all jobs in the
jobs collection.

How can I do that?

Thank you very much guys.
Chris Shepherd - 07 Feb 2008 14:30 GMT
> 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.
> Now my datagridview is bounded to a collection of Person:

[...]

While not a direct answer to your question, you may want to consider using
BindingList<T> instead of a List<T> because by default List<T> will fail to
notify the grid that its data has been changed.

Chris.
Nicholas Paldino [.NET/C# MVP] - 07 Feb 2008 19:01 GMT
Ciro,

   I am not sure that you will be able to use the DataGridView for this to
assign the instance of the job to the property.

   You can try it though.  You will need to make the column of type
DataGridViewComboBoxColumn.  With that, you can set the items in the list,
as well as the values assigned to the property.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> 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
[quoted text clipped - 28 lines]
>
> Thank you very much guys.
Marc Gravell - 07 Feb 2008 19:34 GMT
As Nicholas observes - getting it to tie to the job *instance* is
tricky; however, getting it to tie to a job *key* is trivial - i.e. if
you had:

class Job {
 public string Key {get;set;}
 public string Name {get;set;}
}
class Employee {
 public string JobKey {get;set;}
}

You would give the GataGridView DataSource as the list of employees,
and add a DataGridViewComboBoxColumn with thelist of jobs as the
DataSource, and with DataPropertyName = "JobKey" [relates to
Employee]; DisplayMember = "Name", ValueMember = "Key" [relate to Job]

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.