DataRow to multiple DataTableby condition


#1

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?

Regards.


#2

use Add Data Row activity in if and else condition and add “row” and respective datatable to the properties.


#3

Thank you for your quick response. I tried your suggestion but got an error;

Add data row : This row already belongs to another table.

I resolved this by using ImportRow method. So now i am OK.:slight_smile:

I wonder why Add data row does not work in my case…Does anyone know the reason why? :frowning:


#4

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


#5

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


#6

Since the row belongs to another table, You can try same activity but instead of row use arrayrow and pass row.itemarray


#7

@vvaidya, so interesting… there are many way to resolve issues. Thanks :smile: