views:

531

answers:

3

Is there a way to bind the child properties of an object to datagridview? Here's my code:

public class Person
{
    private string id;
    private string name;
    private Address homeAddr;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public Address HomeAddr
    {
        get { return homeAddr; }
        set { homeAddr = value; }
    }
}

public class Address
{
    private string cityname;
    private string postcode;
    public string CityName
    {
        get { return cityname; }
        set { cityname = value; }
    }
    public string PostCode
    {
        get { return postcode; }
        set { postcode = value; }
    }
}

And I want to show ID, Name, CityName when an object of the type Person is binded to datagridview. Note that CityName is a property of HomeAddr.

A: 

IF you have the DataGridView to AutoGenerateColumns = true, there's really no simple way to do this. Your best bet is to set up the DataGridView ahead of time, and manually populate the DataGridView.

Alternatively, you can implement the ITypedList but that's a bit of a pain if you ask me.

BFree
A: 

Hmm, I've found a way to do this, in FarPoint.

But you can do this in DataGridView, if your object doesn't have a list type property.

Ngu Soon Hui
A: 

You can also look here

http://www.mail-archive.com/[email protected]/msg06383.html

Maciej