Datagridview datasource as List

esko_user
esko_user used Ask the Experts™
on
Hi all, I've a problem with a datagridview.
The data source of the control is a List<Person> mypersonlist ;  where Person is type of a class I've
created.
The class (Person) has some properties like Job, Name, Surname and so on.
Now my datagridview is bounded to a collection of Person when i set my data source as mypersonlist:

Datagridview.Datasource = mypersonlist;

Most of properties of the Person class are strings (like Name and Surname),

Now the question is that the class Person have a property (job).
Furthermore I've got mypersonlist collection
The collection contains all possible jobs(for example, Associate, Accountant.. and so on..).
I would  like to have a combobox column in my datagridview that shows the current job of
the person and gives the user the chance to change the options between all jobs in the
jobs collection.

How can I do that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Enumerate thru your collection and create a dataset out of it. Consider having a Person DataTable and Job, Name, Surname...etc DataColumns. You may customize DateGridView columns to get Combo view (There are plenty of examples out there, just google). Later bind the DataSet to your DataGridView. That should get what you wanted.

Author

Commented:
My collection is a List which has the object of the class Person, and Person inherits a class PersonData. Person has a property Job and Persondata has the properties like Name, Surname etc.  how should i enumerate this list so that the data grid view is filled with the values like,

Job                   Name           surname

Associate          Vinod            kothari
Accountant
Commented:
If Person inherits PersonData then you can very well access the members of PersonData by using Person instance, right? I've written the rough code below. That should get you started.


// Step-1: Create a DataTable and define Data Columns
DataTable myTable = new DataTable();
myTable.Columns.Add(new DataColumn("Name", typeof(string)));
myTable.Columns.Add(new DataColumn("Job", typeof(string)));
:
:
 
// Step-2: Enumerate thru collection and add data to DataTable
foreach(Person person in PersonCollection)
{
 DataRow myRow = myTable.NewRow();
 myRow["Name"] = person.Name;
 myRow["Job"] = person.Job;
 :
 :
 myTable.Rows.Add(myRow);
}
 
Step-3: Create a DataSet instance and add DataTable to it
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(myTable);
 
Step-4: Set the DataSet as DataSource for DataGridView.

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial