2

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.

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Chris - I've already added the columns at design time and they appear at run time, just that they're empty. Is it better practice to add columns at trun time? I even named the colums to match the elements in the XML file (not sure if this is necessary?)
Ok I commented out the line dataGridViewStatement.AutoGenerateColumns = false; and the grid now shows 8 rows with 7 blank columns then 7 columns with the data from the XML, so I guess it's half working now. Does this mean I shouldn't define the columns at design time? Is there a way to get the data displayed in the columns defined at design time?
Well in my case I add the columns at run time via reflection, but I've got a data grid that can display different information depending on the dll loaded.
It should be possible to map the columns at design time - that's certainly something I've done a lot to get the right arrangements and formatting - so the question is why don't your defined columns match up to your data fields.
Ya that's what I was trying to get at, why doesn't the Grid match the XML fields to the design time columns. I just double checked and all the columns are named the same as the fields in the XML (again not sure if this is required). Maybe there's a property or something else I need to set somewhere to do this. If anyone knows of a good introduction tutorial to the DataGridView then I'm all ears!
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.