Click to See Complete Forum and Search --> : Render 'object in object' in DataGridView ?


deisler
February 7th, 2009, 06:36 AM
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.

JonnyPoet
February 12th, 2009, 05:21 PM
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

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 ?