Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am trying to populate Datagridview form class objects
Please see the sample code below

Code:
C#
public class Customer
{
  public string FirstName { get; set;}
  public string LastName { get; set;}
  public Address mAddress { get; set;}
}

public class Address
{
  public string Street { get; set;}
  public string PostalCode { get; set;}
  public string City { get; set;}
}


public List<customer>  SelectCustomer()
{
       List<customer>  CustomerList =  new    List<customer>  ();
       
  while(dr.Read())
     {
  Customer  mCustomer =new Customer();
  mAddress= new  Address();
  mCustomer.FirstName= dr["FirstName"].ToString();
  mCustomer.FirstName= dr["LastName"].ToString();
  mAddress.Street= dr["Street"].ToString();
  mAddress. PostalCode = dr["PostalCode"].ToString();
  mCustomer. mAddress= mAddress;

CustomerList.Add(mCustomer);
     }
   
       return CustomerList;
}


I have bind the Datagridview using List<customer> ,
It shows customer data but in the address column it only shows object reference or no data. I have changed the data property to mAddress. Street and still it does not show the value in it.

Will appreciate any help thanks in advance.
Posted
Updated 24-Feb-15 17:01pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Feb-15 14:45pm    
You are not showing any code using DataGridView.DataSource, DataMember or Value, or some combination of them. Without it, you are not populating anything, or just not showing it.
—SA
chan200uk 24-Feb-15 16:01pm    
Many thanks for the reply
I am using the code as under,
datagridview1.datasource=CustomerList;

Then I add columns in the datagridview and assign data property name. I assigned FirstName to the first column, it worked, but when I set data property name as "Street" it shows object reference only. It does not show the value in that object

1 solution

I think this little tutorial might be helpful.
C# Tutorial - Binding a DataGridView to a Collection[^]

It creates a class, Cars, and creates a list of cars, List<cars> and then binds the list to a DataGridView.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900