Adding a row from one table to another
-
I was doing this dt.Rows.Add(ds.Tables[0].Rows[1]); Where dt is one table and ds.Tables[0] is another table. Now the above statement throws up an error stating that "the row already belongs to one table" if i define an ArrayList ArrayList al = new ArrayList() and do al.Add(ds.Tables[0].Rows[1]) this one works. Does any one know why the first one doesn't work Probably the round about way is creating a new datarow and getting the values from the ds.Tables[0] for that row and rebuilding the new row for the dt table. But dont you guys think it make sense to directly copy the whole row from one datatable to another. How do i do it. Thanks kal
-
I was doing this dt.Rows.Add(ds.Tables[0].Rows[1]); Where dt is one table and ds.Tables[0] is another table. Now the above statement throws up an error stating that "the row already belongs to one table" if i define an ArrayList ArrayList al = new ArrayList() and do al.Add(ds.Tables[0].Rows[1]) this one works. Does any one know why the first one doesn't work Probably the round about way is creating a new datarow and getting the values from the ds.Tables[0] for that row and rebuilding the new row for the dt table. But dont you guys think it make sense to directly copy the whole row from one datatable to another. How do i do it. Thanks kal
The first one doesn't work because a DataRow may only belong to ONE and only one DataTable. The second one works because an ArrayList is not a DataTable hence nobody cares about it. It would make a lot of sense to copy the row from one table to another but what would happen if you change the values of the row in table A. the values would also be changed in table B because they're sharing a row. Maybe you want it to act like this but i wouldn't recommend trying it like this. Instead you should do it as you've already mentioned and copy the important data from the row of table A to a NEW row of table B. Good luck, mik