Hi,
I want to convert DataRow to multiple DataTable distingished by condition. I tried to do this way;
1)DataRows dtArray = new DataRows, add value into DataRow
2)DataTable dt1 = new DataTable
3)DataTable dt2 = new DataTable
4)in ForEach row in dtArray, using If condition, do assign dtArra to dt1, dt2
—If row.Item(“COUNTRY”).ToString = “Japan”
------Then dt1 = dtArray.CopyToDataTable()
------Else dt2 = dtArray.CopyToDataTable()
It doesn’t work. The result of dt1 and dt2 is the same, you know…
What I wanna do is to collect “Japan” row to dt1, others row to dt2. How can I do this?
I guess that the reason is that “Add Row” only works if the row that you try to add does not belong to another table within your program.
DataTable dt1=new DataTable();
DataRow dr1=dt1.NewRow();
DataTable dt2=new DataTable();
dt2.Rows.Add(dr1); // will give you error already dr1 belongs to another datatable in that //case you can do like this
dt2.ImportRow(dr1); // safe
dt1.Rows.Add(dr1); // safe as dr1 Row belongs to DataTable1 so no exception raise
I guess “Add” only works if the row that you try to add does not belong to another tables within your program. Please see the example at below
DataTable dt1=new DataTable();
DataRow dr1=dt1.NewRow();
DataTable dt2=new DataTable();
dt2.Rows.Add(dr1); // will give you error already dr1 belongs to another datatable in that //case you can do like this
dt2.ImportRow(dr1); // safe
dt1.Rows.Add(dr1); // safe as dr1 Row belongs to DataTable1 so no exception raise