Binding a DataGridViewColumn to a Layer 2 Object

I would like to bind a column in my DataGridView class to a record in a second level object in C # using .NET 4.0. For instance:

I have an object A:

public class A
{
   public long id;
   public B bClass;
}

and object B

public class B
{
   public long id;
   public string name;
}

Is there a way to declare a list of class A as a data source in a DataGridView, but bind one of the columns to the name attribute in class B?

I redid it a bit, but I hope it doesn’t bother. Thanks for the help!

+5
source share
2 answers

Yes.
When you bind, you do this:

grid.DataSource = MyAList;
grid.DataMember = "bClass";
grid.DataBind();

Now that you are attached, you are evaluating members of class B.

0
source

. , : datagrid?

, C gui-layer, . , A:

public class C
{
    public C(A a)
    {
        Id = a.Id;
        Bid = a.bClass.Id;
        Bname = a.bClass.Name;
    }

    public long Id;
    public long Bid;
    public string Bname;
}
+1

All Articles