簡體   English   中英

將嵌套對象綁定到DataGridView BindingSource

[英]Bind nested object to DataGridView BindingSource

我有一個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

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

您可以綁定到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;

請注意,列是從第一個記錄生成的,就像您所做的那樣。 然后我假設第一個記錄的屬性與所有其他記錄的屬性相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM