简体   繁体   English

使用 DataSource 在 DataGridView 中显示 XML 数据

[英]Displaying XML data in DataGridView using DataSource

I have a form with a DataGridView and I want to load data from an XML file into the Grid using a DataSet.我有一个带有 DataGridView 的表单,我想使用 DataSet 将 XML 文件中的数据加载到网格中。 I create a DataSet, load the XML into the DataSet, then assign the DataSet to the DataSource property of the Grid:我创建了一个 DataSet,将 XML 加载到 DataSet 中,然后将 DataSet 分配给 Grid 的 DataSource 属性:

private void formAccountHistory_Load(object sender, EventArgs e)
{
    // Load the DataSet that represents the offline version of the database.
    AccountHistoryDS = new DataSet("TicketAccountHistory");

    AccountHistoryDS.ReadXmlSchema("TicketsAccountHistory.xsd");
    AccountHistoryDS.ReadXml("TicketsAccountHistory.xml", XmlReadMode.Auto);
    AccountHistoryDS.Locale = System.Globalization.CultureInfo.CurrentUICulture;

    dataGridViewStatement.AutoGenerateColumns = false;
    dataGridViewStatement.DataSource = AccountHistoryDS;
    dataGridViewStatement.DataMember = "Line";
}

However the data doesn't display in the Grid.但是数据不会显示在网格中。 I have 8 rows in the XML file and the Grid creates 8 rows alright but they are all blank.我在 XML 文件中有 8 行,网格创建了 8 行,但它们都是空白的。 When I debug the code I can see the data in the DataSet so it seems to be loading it correctly to that point, just not displaying it in in the Grid.当我调试代码时,我可以看到 DataSet 中的数据,所以它似乎正确加载到那个点,只是没有在网格中显示它。 The XML file I use is below - it is well formed and validates against its schema:我使用的 XML 文件如下 - 它格式良好并根据其架构进行验证:

<?xml version="1.0" standalone="yes"?>
<TicketsAccountHistory>
    <Line>
        <colID>03/09</colID>
        <colStartEnd>14/01/2009-20/01/2009</colStartEnd>
        <colDate>14/01/2009</colDate>
        <colType>Period 03/09 - opening balance</colType>
        <colDR></colDR>
        <colCR></colCR>
        <colBalance>0.00</colBalance>
    </Line>
    <Line>
        <colID>03/09</colID>
        <colStartEnd>14/01/2009-20/01/2009</colStartEnd>
        <colDate>20/01/2009</colDate>
        <colType>Sales Invoice (Ref: MRO-S-03/09)</colType>
        <colDR>1000</colDR>
        <colCR></colCR>
        <colBalance>1000.00</colBalance>
    </Line>
    <Line>
        <colID>03/09</colID>
        <colStartEnd>14/01/2009-20/01/2009</colStartEnd>
        <colDate>20/01/2009</colDate>
        <colType>Commission Invoice (Ref: MRO-C-03/09)</colType>
        <colDR></colDR>
        <colCR>100.00</colCR>
        <colBalance>900.00</colBalance>
    </Line>
    <!-- 5 more rows similar to this -->
</TicketsAccountHistory>

Can anyone tell me what I might be doing wrong?谁能告诉我我可能做错了什么? I'm new to .NET 3.5 and the DataGridView and I don't know how what events are fired when a Grid is populated, if there should be code in any of those events, etc. Any help appreciated.我是 .NET 3.5 和 DataGridView 的新手,我不知道填充 Grid 时如何触发哪些事件,是否应该在任何这些事件中包含代码等。感谢任何帮助。

Cheers, Ciaran.干杯,西亚兰。

You've got the statement:你有这样的声明:

 dataGridViewStatement.AutoGenerateColumns = false;

This means that the DataGridView won't have any columns.这意味着 DataGridView 不会有任何列。 Either set it to true or insert some code to add the columns.将其设置为true或插入一些代码来添加列。

After you manually generate the columns and have them named the same as your XML fields, try this:在您手动生成列并将它们命名为与您的 XML 字段相同的名称后,请尝试以下操作:

    For Each col As DataGridViewColumn In dataGridViewStatement.Columns
        col.DataPropertyName = col.Name
    Next

When you manually edit the columns in your grid, you can set the ColumnName to whatever you want, but the important field to set is the DataPropertyName, which is what must match the element names in your XML.当您手动编辑网格中的列时,您可以将 ColumnName 设置为您想要的任何值,但要设置的重要字段是 DataPropertyName,它必须与 XML 中的元素名称匹配。 For convenience, I usually set both of them to be the same as the XML element name.为方便起见,我通常将它们都设置为与 XML 元素名称相同。

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

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