Results 1 to 5 of 5

Thread: [2.0] DataGridView Objects Probs.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    [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,...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] DataGridView Objects Probs.

    Then you probably want to override the ToString() method and have it return the surname property.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width