i'm developing a faq session of our website and I'm facing some
problems handling xml files and DataSet relations
First off all, here's the structure of my xml file:
<?xml version="1.0"?>
<questions>
<topic order="1" title="General">
<item order="1" question="blablabla?">
<answer>blablabla</answer>
</item>
<item order="2" question="blablabla?">
<answer>blablabla</answer>
</item>
</topic>
<topic order="2" title="Police Office">
<item order="1" question="blablabla?">
<answer>blablabla</answer>
</item>
</topic>
<topic order="2" title="Ensurance">
<item order="1" question="blablabla?">
<answer>blablabla</answer>
</item>
<item order="2" question="blablabla?">
<answer>blablabla</answer>
</item>
<item order="3" question="blablabla?">
<answer>blablabla</answer>
</item>
</topic>
and so on...This structure is acceptable? I'm forgetting something? I
know it doesnt have an schema, but I havent learned how to build one
yet.
Now the code
I have two datagrids, as the MS sample (in
http://support.microsoft.com/default...b;en-us;308485)
but I have changed something to simplify
private void Page_Load(object sender, System.EventArgs e)
{ // Put user code to initialize the page here
string filename = Server.MapPath(".");
XmlDocument doc = new XmlDocument();
doc.Load(filename + "\\perguntas.xml");
// this function reads the xml file and create 3 tables (topics, itens
and answers) whithin a dataset ds
ds = ReadXml(doc);
ds.Relations.Add("rel", ds.Tables["topics"].Columns["order"],
ds.Tables["itens"].Columns["order"]);
grid1.DataSource = ds;
grid1.DataMember = "topics";
grid1.DataKeyField = "order";
grid1.DataBind(); }
up to here, everything is ok, I can load the xml file and show the
first level of it (the <topic) tag's colection with a '+' at the left
side where I want the child elements (in this case the <itens> tags)
Now the problem. When I click in the '+' column the event
OnSelectedIndexChanged is fired.
//delegate for OnSelectedIndexChanged of Grid1
protected void grid1_OnSelectedIndexChanged(System.Object sender,
System.EventArgs e) {
this.Showdetailgrid(); }
Following, the function that handles the event (where the error come
out)
// handle the event
protected void Showdetailgrid() {
if(grid1.SelectedIndex != -1)
{
DataView parentrows = new DataView();
DataView childrows;
DataRowView currentparentrow;
parentrows.Table = ds.Tables["itens"];
currentparentrow = parentrows[grid1.SelectedIndex];
childrows = currentparentrow.CreateChildView("rel"); ***
grid2.DataSource = childrows;
grid2.DataBind();
grid2.Visible = true;
}
else
grid2.Visible = false;
}
When the line with *** is executed I got the following error:
System.ArgumentException: The relation is not parented to the table to
which this DataView points.
Could someone help me please?