Last post Sep 23, 2011 10:57 AM by Boddah
Sep 14, 2011 08:06 AM|Boddah|LINK
I'm trying to read an XML file into a datagrid. However, it seems that the column names of the table in the XML get lost so I can't get assign the data to the grid.
The XML is read using an XSD schema. It's structure is a little complex, with 2 nested tables ("sections" and "persons")
Using the GetChildRows I am able to retrieve the table I want, but the columns have only indexes, no names. For example, the first column is named "id" in the XSD, but I can only retrieve its contents using the column index not its name.
This is a problem when I assign this column to the grid, because the Databinder.Eval method requires the column name and not the index. When I do it like this: DataBinder.Eval(Container.DataItem, "id"), I get an error saying there's no column with that name.
I think I should perhaps parse the table, but I don't really know how to do this. I thought the XSD schema took care of this, but I guess it doesn't. Can anyone help me out? (I hope I explained my problem clearly
Here is my code:
// Read XML into dataset
dsResults = new DataSet();
// Assign dataset table to grid
dgResults.DataSource = dsResults.Tables["section"].Rows.GetChildRows("section_persons").GetChildRows("persons_person");
And the datagrid code:
<asp:datagrid id="dgResults" runat="server" BorderWidth="0" autogeneratecolumns="False">
<asp:hyperlink id="hlPerson" runat="server" NavigateUrl='<%# "persondetails.aspx?id=" + DataBinder.Eval(Container.DataItem, "id") %>' Text='<%# DataBinder.Eval(Container.DataItem, "id") %>'></asp:hyperlink>
Sep 14, 2011 01:50 PM|Boddah|LINK
Maybe I didn't explain myself properly. Basically, I'm trying to bind the results of the GetChildRows method to the datagrid.DataSource. This doesn't seem to work, as the datarow array doesn't contain any field (column) names, so I can't use the databinder.Eval
How can I make this work?
Alternatively, is it possible to databind the fields index-based instead of column name-based?
Thank you for any help
Sep 15, 2011 10:44 PM|Decker Dong - MSFT|LINK
If you want to get a ChildRows, you should first create relationship between two or more tables by using:
And then do something like this:
foreach(DataRow row in DataSet1.Tables.Rows)
Sep 23, 2011 10:57 AM|Boddah|LINK
The relations are created the moment I read the XSD schema, so that part is covered. The problem I had is showing the childrows directly in a datagrid.
I finally solved it like this:
// Clone table definition
DataTable table = dsResults.Tables["person"].Clone();
// Get all persons in section
DataRow rows = dsResults.Tables["section"].Rows.GetChildRows("section_persons").GetChildRows("persons_person");
// Assign persons to copy of table
foreach (DataRow row in rows)
It just appeared a little strange to me that the GetChildRows method loses the column definition, and that I have to copy the results row by row to a copy of the original table before I can assign it to a datagrids datasource. Maybe there is a better solution
than how I did it, but at least this works.