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