[英]Winforms datagrid :How the bind datagrid with list of derived classes
I'm facing a problem with winforms datagrids binding. 我面临Winforms DataGrid绑定的问题。
i have a base class A, this class is inherited by A1 and A2 我有一个基类A,该类由A1和A2继承
public class A
{
public string P {get;set;}
}
public class A1 : A
{
public string P1 {get;set;}
}
public class A2 : A
{
public string P2{get;set;}
}
i have also a list i want to display in a grid 我还有一个要显示在网格中的列表
List<A> Mylist = new List<A>();
MyList.add(new A1());
MyList.add(new A2());
The grid has 2 columns, the first column displays the value of P, the second column displays P1 or P2. 网格有2列,第一列显示P的值,第二列显示P1或P2。
How can i do this ? 我怎样才能做到这一点 ?
Regards, 问候,
Moon 月亮
Design your classes like this: 像这样设计课程:
public class A
{
public string P { get; set; }
public virtual string VirtualString { get; set; }
}
public class A1 : A
{
public string P1 { get; set; }
public override string VirtualString
{
get { return P1; }
set { P1 = value; }
}
}
public class A2 : A
{
public string P2 { get; set; }
public override string VirtualString
{
get { return P2; }
set { P2 = value; }
}
}
You need a common base for any custom behavior in your derived classes. 对于派生类中的任何自定义行为,都需要一个通用的基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.