Copy just some rows from a datatable to another datatable.

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

I have a datatable with a field in it that tells me where the data belongs
(let's say the values are 'A' and 'B'). I want to split the table into two
other tables where in one there are only the 'A' records and the other one
has only the 'B' records.

What's the easiest way to do it? I looked at a dataview but it has no copy,
it has copyto.

TIA - Jeff.
 
You can use DataTable.Select to get just the matching rows for both A and B.
That should do it for you.
 
If you use ADO.NET 2.0, you can use following method:

DataView dv = datatable.DefaultView;
dv.RowFilter = "FieldName='A'";
DataTable tableA = dv.ToTable();
dv.RowFilter = "FieldName='B'";
DataTable tableB = dv.ToTable();

But in ADO.NET 1.1, there isn't DataView.ToTable method. You need loop thru
dataview and 'copy' row to a datatable.

HTH

Elton Wang
 
Back
Top