C# DatagridView with List<t> as DataSource

C

Ciro

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.
 
C

Chris Shepherd

Ciro said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Marc Gravell

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
 
C

Ciro

Thank you all guys! It works!
I just had to add a .Find delegate in the set method of the JobKey property
to allow the user to change the instance of Job via the dropdownlist.
I just wrote:

string JobKey
{
get{return _job.Key }
set
{
Job newJob=
Jobs.Find(delegate(Job myNewJob){return
(myNewJob.Key==value);});
if (newJob!=null)
_job=newJob;
}
}

Thanks again.

Nicholas Paldino said:
It's not that hard to tie to the instance, really, as long as the
instance exposes a property that references itself, then you can use that
for the ValueProperty on the DataGridViewComboBox which will have the drop
down.

I've attached a very rudimentary, dirty VS.NET 2008 project which
displays how it can be done.

To make this cleaner, you can also create a wrapper which exposes the
self-reference if you don't want to dirty up the original object design
(which is what I would recommend to do).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Gravell said:
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
 

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