简体   繁体   English

将嵌套对象绑定到DataGridView BindingSource

[英]Bind nested object to DataGridView BindingSource

I have an XML file that I am able to bind to bind to the DataGridView using the following code: 我有一个XML文件,我能够绑定到使用以下代码绑定到DataGridView:

XML 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# 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
    /*
}

The code above works perfectly fine, the problem I have is when I added a nested tag into the XML which I have to display in another DataGridView 上面的代码工作得非常好,我遇到的问题是当我在XML中添加嵌套标记时,我必须在另一个DataGridView中显示

The new XML structure looks like this: 新的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>

I'm dynamically creating the DataGridView columns based on the property.name so it looks like this: 我正在基于property.name动态创建DataGridView列,所以它看起来像这样:

StartDate    |     EndDate    |     Description
             |                |     

The problem right now is actually filling up this filling up this DataGridView with the values from the property.name in the XML 现在的问题实际上是用XML中的property.name中的值来填充此DataGridView

Just to give you an idea, this is how the class is structured. 只是为了给你一个想法,这就是课程的结构。

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;
}

Here's what I've tried so far: 这是我到目前为止所尝试的:

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

And this is the result: 这就是结果:

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

You see that it added new columns and filled in the rows instead of filling up the rows for the columns I created. 您会看到它添加了新列并填充了行,而不是填充我创建的列的行。

You could bind to a DataTable . 您可以绑定到DataTable You would have the ability to generate columns from an array of objects, which can come from your name attributes. 您可以从一组对象生成列,这些列可以来自您的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;

Note that the columns are generated from the first record, as you had done. 请注意,列是从第一个记录生成的,就像您所做的那样。 Then I assume that the first record's properties are the same as all the other records' properties. 然后我假设第一个记录的属性与所有其他记录的属性相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM