|
-
Jun 7th, 2007, 11:11 AM
#1
Thread Starter
Hyperactive Member
[2.0] DataGridView Objects Probs.
Hi
Suppose I have a Class Person, with a property "Surname".
I have a dataTable such as:
Code:
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("Surname",typeof(Person));
dt.Columns.add(dc);
Create an Instance of Person:
Code:
Person prs = new Person();
prs.Surname = "Rauland";
I now, create a dataRow:
Code:
DataRow dr = dt.CreateRow();
dr["Surname"] = prs;
dt.rows.add(dr);
I now use this dataTable as a dataSource to a dataGridView, named dbTest;
This grid has a column named, "Surname".
And have set the DataPropertyName to "Surname".(Meaning that it will display in this column the DataTable´s column with name "Surname")
Code:
dbTest.DataSource = dt;
The only thing it displays the type of object, in this case, Person.
Is there a way to set what in other controls is called "DisplayMember", the DataTables´s column named "Surname", to display the Person´s property "Surname".
Thanks!!
Last edited by Rauland; Jun 7th, 2007 at 04:23 PM.
Reason: Code correction,...
-
Jun 7th, 2007, 06:14 PM
#2
Re: [2.0] DataGridView Objects Probs.
You've got a DataRow with a column named "Surname", yet you're assigning an entire Person object to it:
Code:
dr["Surname"] = prs;
If you want that column to contain a surname then you need to assign the Surname property of the Person to it, not the whole Person.
-
Jun 11th, 2007, 07:04 AM
#3
Thread Starter
Hyperactive Member
Re: [2.0] DataGridView Objects Probs.
Hi Thanks jmcilhinney,
YOur right, I only had to assign the Surname Property.
But certain controls, have a display member, that you can use ,to make it display the property your intersted in.
ComboBoxes, etc,... set the DisplayMember to "SurnameName", assign a List of Person Objects to it and the control knows which property to display, in this case "Surname".
I was just asking my self if there was a property, similiar to DisplayMember, but for DataColumns(which aren´t controls, or are they?? ). So I could add the whole object, and get it to display the name.
I think I can define datacolumns,defining the type it will accept;
Code:
DataColumn dc = new DataColumn("Surname",typeof(Person));
Doing it the way you suggested solved the problem, but, in that case the datacolumn would accept strings Person.Surname. Which isn´t a great problem, but would of been nice to have person objects, so any work I did on the column would be with person objects not strings.
Maybe I´m over complicating things,..
Thanks
Rauland
-
Jun 11th, 2007, 03:39 PM
#4
Re: [2.0] DataGridView Objects Probs.
Then you probably want to override the ToString() method and have it return the surname property.
-
Jun 11th, 2007, 07:19 PM
#5
Re: [2.0] DataGridView Objects Probs.
What mendhak says is quite correct, but I don't really see why you would want a Person object in a column anyway. In that case it sounds to me like you should have two DataTables related by the ID of a Person record.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|