将嵌套对象绑定到DataGridView BindingSource

时间:2017-04-04 18:12:38

标签: c# xml winforms datagridview

我有一个XML文件,我可以使用以下代码绑定到DataGridView:

XML

<entity>
    <primitive name"Name1" type="Type1" index="Index1" nullable="true" isKey="false" />
    <primitive name"Name2" type="Type2" index="Index2" nullable="true" isKey="false" />
 </entity>

C#

//The creation of columns here is explicit which is actually dynamic which I will show later
DataGridViewColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.DataPropertyName = "Name";
nameColumn.Name = "Name";
dataGridViewPrimitives.Columns.Add(nameColumn);

DataGridViewCheckBoxColumn keyColumn = new DataGridViewCheckBoxColumn();
keyColumn.DataPropertyName = "IsKey";
keyColumn.Name = "Is Key";
dataGridViewPrimitives.Columns.Add(keyColumn);

//all other columns here

private void Bind()
{
    dataGridViewPrimitives.DataSource = null;
    bindingSourcePrimitives.DataSource = entity.Primitives;
    dataGridViewPrimitives.DataSource = bindingSourcePrimitives;
    /*
     * entity.Primitives is a collection of primitives parsed somewhere
     * but basically entity is an object with a member Collection<Primitive> Primitives
    /*
}

上面的代码完全正常,我遇到的问题是当我在XML中添加嵌套标记时,我必须在另一个DataGridView中显示

新的XML结构如下所示:

<entity>
    <primitive name"Name1" type="Type1" index="Index1" nullable="true" isKey="false" />
    <primitive name"Name2" type="Type2" index="Index2" nullable="true" isKey="false" />
    <staticData>
        <records>
            <record>
                <property name="StartDate" value="04/04/2017" />
                <property name="EndDate" value="04/04/2017" />
                <property name="Description" value="Very Good" />
            </record>
        </records>
    </staticData>
 </entity>

我是基于property.name动态创建DataGridView列的,所以它看起来像这样:

StartDate    |     EndDate    |     Description
             |                |     

现在的问题实际上是用XML中的property.name中的值填充这个DataGridView的填充

只是为了给你一个想法,这就是课程的结构。

public class Entity
{
    public Collection<Primitive> Primitives;
    public Collection<StaticData> StaticData;
}

public class StaticData
{
    public Collection<Records> Records;
}

public class Records
{
    public Collection<Record> Record;
}

public class Record
{
    public Collection<Property> Property;
}

这是我到目前为止所尝试的内容:

 bindingSourceStaticData.DataSource = Entity.StaticData.FirstOrDefault().Records.FirstOrDefault().Record.FirstOrDefault().Property;
 dataGridViewStaticData.DataSource = bindingSourceStaticData;

这就是结果:

StartDate    |     EndDate    |     Description    |    Name        |    Value
             |                |                    |    StartDate   |    04/04/2017
             |                |                    |    EndDate     |    04/04/2017
             |                |                    |    Description |    Very Good

您看到它添加了新列并填充了行,而不是填充我创建的列的行。

1 个答案:

答案 0 :(得分:0)

您可以绑定到DataTable。您可以从一组对象生成列,这些列可以来自您的name属性。

// populate DataTable columns
DataTable dt = new DataTable();
Collection<Property> properties = entity
    .StaticData.FirstOrDefault()
    .Records.FirstOrDefault()
    .Record.FirstOrDefault()
    .Property;
// from the collection of xml name attributes in the first record
dt.Columns.AddRange(properties.Select(p => new DataColumn(p.Name)).ToArray());

// populate DataTable rows
Collection<Record> records = entity
    .StaticData.FirstOrDefault()
    .Records.FirstOrDefault()
    .Record;
// create a row from each record, with the values from the xml value attribute
foreach (Record r in records)
{
    dt.Rows.Add(r.Property.Select(p => p.Value).ToArray());
}

// bind to the datatable
dataGridView1.DataSource = dt;

请注意,这些列是从第一条记录生成的,就像您所做的那样。然后我假设第一个记录的属性与所有其他记录的属性相同&#39;属性。