DGV数据绑定以及“嵌套”二级属性类中的集合?

时间:2019-04-28 04:47:19

标签: c# data-binding datagridview nested datasource

是否可以将这种数据结构绑定到DGV?

public class ClassA
{
    public string propertyA { get; set; }
    public string propertyB { get; set; }
    public ClassB propertyC { get; set; }
}

// This class acts like a preset uniform list, just to add extra columns
public class ClassB
{
    public static List<string> namesForEveryClassC { get; private set; } // Also every column header and set by the user
    private List<ClassC> data { get; set; } // Bind this

    public ClassC this[string name]
    {
        get
        {
            foreach (ClassC item in this.data)
                if (item.name == name)
                    return item;
            return null;
        }
    }

    public ClassB()
    {
        foreach (string name in namesForEveryClassC)
            data.Add(new ClassC(name));
    }
}

public class ClassC
{
    public string name { get; set; } // This is only to identify columns
    public int value { get; set; } // Binded in the DataGridView as a cell and able to be set by the user

    public ClassC(string aName)
    {
        this.name = aName;
        this.value = 0;
    }
}

ClassB类似于DGV列的下一部分,静态字符串集合用作列标题的名称,还可以从用户从ListBox进行编辑,应该绑定到每一列的是int中的int值。 ClassC。 DGV基本上应该像...

 Class A properties      namesForEveryClassC string items as headers
+-----------+-----------+--------+--------+--------+ ... +--------+
| PropertyA | PropertyB | ValueA | ValueB | ValueC | ... | ValueN |
+-----------+-----------+--------+--------+--------+ ... +--------+
|    ...    |    ...    |    2   |    4   |   0    |     |    7   |
 Class A values          int values in every ClassC from ClassB collection data

哪种方法应该好? ClassB是否继承BindingSource?使用ITypedList接口和PropertyDescriptor?手动设置每一列?

我的主要问题是使用ClassB的索引器访问值。

0 个答案:

没有答案