4

I am having trouble displaying the XML on my c# WinForm.

My XML file looks like this:

<HotelDatabase>
    <Hotel Name="ABC" Location="XYZ">
        <Room Type="Single" Count="5" Price="2000" />
        <Room Type="Superior" Count="3" Price="4000" />
    </Hotel>
    <Hotel Name="DEF" Location="LMN">
        <Room Type="Single" Count="5" Price="2000" />
        <Room Type="Superior" Count="3" Price="4000" />
    </Hotel>
</HotelDatabase>

My Code for displaying the data in DataGridView looks like this:

var dataSet = new DataSet();
dataSet.ReadXml(Properties.Settings.Default.HotelDB);
dataGridViewHotelList.DataSource = dataSet.Tables[0];

When I run this code, only Name and Location is displayed. I want all the attributes in the Hotel element and its child element to be displayed in a datagridview.

2
  • Hotel has only this 2 attributes, do you mean the childs? Commented Feb 25, 2016 at 16:10
  • @Pazi01 Yes I mean all the attributes in the hotel elements and as well as its child attributes Commented Feb 25, 2016 at 16:11

2 Answers 2

4

You are loading XML data to grid correctly, but you should know when you bind DataGridView to a DataTable it shows columns of the data table not the columns of related tables.
You can not show a relation in a single DataGridView. To show child items of a hotel, you can use another DataGridView in the same form to show child items of hotel and bind the second grid to rooms of a hotel:

var ds = new DataSet();
ds.ReadXml("path to xml file");
dataGridView1.DataSource = ds.Tables["Hotel"]; //Tables[0]
dataGridView2.DataSource = ds.Tables["Hotel"]; //Tables[0]
dataGridView2.DataMember = "Hotel_Room"; //ds.Tables[0].ChildRelations[0].RelationName;

This way you will have a master-detail relation between your data grids. By click on each hotel row, you will see its rooms.

You also have some other options, for example:

  • You can create a DataGridViewButtonColumn to the grid that opens a form which shows a grid containing rooms of selected hotel.
  • You can use a DataGrid control which can show a link to child items of a row. To do so, it's enough to set hotels as data source of data grid control: this.dataGrid1.DataSource = ds.Tables["Hotel"];
Sign up to request clarification or add additional context in comments.

2 Comments

what does this line do dataGridView2.DataMember = "Hotel_Room";
It makes the datagridview use that relation to find child rooms of current hotel. To learn more take a look at Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls
0

You can convert the XML to a datatable and the just plug it in to your gridview with DataTable.ReadXML(), here is example code from MSDN:

private static void DemonstrateReadWriteXMLDocumentWithString()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintValues(table, "Original table");

    string fileName = "C:\\TestData.xml";
    table.WriteXml(fileName, XmlWriteMode.WriteSchema);

    DataTable newTable = new DataTable();
    newTable.ReadXml(fileName);

    // Print out values in the table.
    PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
    // Create a test DataTable with two columns and a few rows.
    DataTable table = new DataTable(tableName);
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table.Columns.Add(column);

    // Add ten rows.
    DataRow row;
    for (int i = 0; i <= 9; i++)
    {
        row = table.NewRow();
        row["item"] = "item " + i;
        table.Rows.Add(row);
    }

    table.AcceptChanges();
    return table;
}

private static void PrintValues(DataTable table, string label)
{
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column]);
        }
        Console.WriteLine();
    }
}

And if you need to know how to bind your datagridview to a datatable, here, is a link on how to do that.

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.