CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    1

    Render 'object in object' in DataGridView ?

    I have a generic list List<Order>, in which Order object has some common property such as OrderId,TotalValue and property that map to another object, called Customer{CustomerId, CustomerName}. Snippet of code for clarification :
    class Order
    {
    string OrderId; // Property
    double TotalValue; // Propery
    Customer CustomerOnSign; // Property

    }
    class Customer
    {
    string CustomerId; //Property
    string CustomerName; //Property
    }

    Now I want to populate List<Order> to DataGridView.Datasource and when I try to render Customer column, I set the column's DataPropertyName = "CustomerOnSign.CustomerName" it seems to appear nothing in the grid. If I set the DataPropertyName = "CustomerOnSign", it only appears the Customer object name but not the CustomerName property, which I want.

    Just want to know is there any way to overcome this prob ? Any help is highly appreciated.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Render 'object in object' in DataGridView ?

    Quote Originally Posted by deisler View Post
    I have a generic list List<Order>, in which Order object has some common property such as OrderId,TotalValue and property that map to another object, called Customer{CustomerId, CustomerName}. Snippet of code for clarification :
    class Order
    {
    string OrderId; // Property
    double TotalValue; // Propery
    Customer CustomerOnSign; // Property

    }
    class Customer
    {
    string CustomerId; //Property
    string CustomerName; //Property
    }

    Now I want to populate List<Order> to DataGridView.Datasource and when I try to render Customer column, I set the column's DataPropertyName = "CustomerOnSign.CustomerName" it seems to appear nothing in the grid. If I set the DataPropertyName = "CustomerOnSign", it only appears the Customer object name but not the CustomerName property, which I want.

    Just want to know is there any way to overcome this prob ? Any help is highly appreciated.
    Properties are only to be seen if they are public so you may need to do corrct classes as yor CustomerID and Customername is private in the code you show.

    If you want to use the object you can override its ToString() method of the Customer class like
    Code:
    public class Customer{
       private string customerName;
    ....
     
    public override string ToString(){
     this.CustomerName;
    }
    // The Properties needs to look like this one
    public string CustomerName{
       get{ return this.customerName;}
       set {this.customerName = value;}
    }
    Have you dtried to debug and looked if your class is correctly filled with data ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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

Featured