Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is a code if i want to add rows to a table, so that, table can be used as DataSource for DataGridview but line 8 raises error (error: - This row already belongs to another table). Please help how i could add those row to a table

C#
DataRelation customerOrdersRelation =
    customerOrders.Relations.Add("CustOrders",
    customerOrders.Tables["Customers"].Columns["CustomerID"],
    customerOrders.Tables["Orders"].Columns["CustomerID"]);
DataTable dt=new DataTable();
foreach (DataRow custRow in customerOrders.Tables["Customers"].Rows)
{
   dt.Rows.Add(custRow); // raise error. Line 8

    foreach (DataRow orderRow in custRow.GetChildRows(customerOrdersRelation))
    {
        // some code 
    }
}


Thank U.
Posted
Updated 7-Dec-13 23:38pm
v2
Comments
joginder-banger 8-Dec-13 5:23am    
what's type of error you getting or Name of error.
Brijesh Kr 8-Dec-13 5:38am    
error: - This row already belongs to another table

1 solution

Refer- copy rows from Datatable to another Datatable c#[^].
C#
foreach (DataRow dr in dataTable1.Rows) {
    if (/* some condition */)
        dataTable2.Rows.Add(dr.ItemArray);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900