简体   繁体   中英

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. I create a DataSet, load the XML into the DataSet, then assign the DataSet to the DataSource property of the Grid:

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. 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. The XML file I use is below - it is well formed and validates against its schema:

<?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.

Cheers, Ciaran.

You've got the statement:

 dataGridViewStatement.AutoGenerateColumns = false;

This means that the DataGridView won't have any columns. Either set it to true or insert some code to add the columns.

After you manually generate the columns and have them named the same as your XML fields, try this:

    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. For convenience, I usually set both of them to be the same as the XML element name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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