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 s开发者_开发百科tring 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!
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;
}
Yes.
When you bind, you do this:
grid.DataSource = MyAList;
grid.DataMember = "bClass";
grid.DataBind();
Now when you're binding, you're evaluating to the members of class B.
精彩评论