Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DataGridViewColumn to 2nd level object

I would like to bind a column in my DataGridView class to an entry in a 2nd level object in C# using .NET 4.0. For instance:

I have 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's as the data source on a DataGridView, yet bind one of the columns to the name attribute in Class B?

I distilled this down a little bit, but hopefully this isn't confusing. Thanks for the Help!

like image 492
Patrick McDaniel Avatar asked Nov 15 '22 05:11

Patrick McDaniel


1 Answers

The above solution doesn't work for me. I understood the question more like in this thread: Is it possible to bind complex type properties to a datagrid?

What I did is to implement a class C in the gui-layer that has all the wanted properties in the first level. You can also write a constructor initializing the new class from class 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;
}
like image 95
simaglei Avatar answered Dec 22 '22 07:12

simaglei