从数据源

时间:2017-06-27 11:13:10

标签: c# winforms data-binding datagridview

我有基类和一些继承的类,如下所示:

public abstract class Base
{
    public long ID { get; set; }
    public string Name { get; set; }
    public virtual string Description { get { return "Base"; } }
}

public class A : Base
{
    public override string Description { get { return "A"; } }
}

public class B : Base
{
    private string extraInfo;

    public override string Description { get { return "B"; } }

    public string ExtraInfo
    {
        get { return extraInfo; }
        set { extraInfo = value; }
    }
}

我收集了这些对象,并将此集合设置为DataSource的{​​{1}}。然后,DataGridView的列将由DataGridView类的属性(BaseIDName)创建。有没有办法反映具体的实施属性?

Description

在这种情况下,我还想要类 List<Base> items = new List<Base>(); DataGridView dgv = new DataGridView(); dgv.DataSource = new BindingList<Base>(items); 的{​​{1}}属性列。

1 个答案:

答案 0 :(得分:0)

继承B类:

public class Base
{
    public long ID { get; set; }
    public string Name { get; set; }
    public virtual string Description { get { return "Base"; } }
}

public class A : Base
{
    public override string Description { get { return "A"; } }
}

public class B : Base
{
    private string extraInfo;

    public override string Description { get { return "B"; } }

    public string ExtraInfo
    {
        get { return extraInfo; }
        set { extraInfo = value; }
    }
}