Below is the XML file used:


As you can see the "User" table contains an "Order" Table.  In XML which orders belong to which user is very clear, however each order entry does not have a "Name" key which will link the order to the user in traditional relational database like so:

The xml file is currently put into the 2 bindingsources like so:

            string relation = datasetUser.Relations[1].RelationName;
            bindsourceUser.DataSource = datasetUser;
            bindsourceUser.DataMember = "User";
            bindsourceOrder.DataSource = bindsourceUser;
            bindsourceOrder.DataMember = relation;

What I want to do is when I add a new user it automatically adds a few orders under that User name, but I don't want to put the <Name> element under <Order> to keep the XML clean.  Right now I add a new row by handling the bindingsourceUser_ListChanged event.

How would I do this   Thanks.

Re: Windows Forms Data Controls and Databinding How to add new child row to xml set with bindingsource WITHOUT an id key?


When dataset read nested XML, in case your xml, a Key "User_Id" is automatically added by dataset to each User table and Order table.

Try below code.

DataRow myrow;
DataTable tbl1 = datasetUser.Tables["User"];
DataTable tbl2 = datasetUser.Tables["Order"];

myrow = tbl1.NewRow();
myrow["Name"] = "NewName";
int n =(int) myrow["User_Id"];
myrow = tbl2.NewRow();
myrow["OrderType"] = 7;
myrow["OrderName"] = "NewItem";
myrow["User_Id"] = n;