Home All Groups Group Topic Archive Search About

Copy just some rows from a datatable to another datatable.

Author
2 Jan 2006 8:50 PM
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.

Author
2 Jan 2006 9:04 PM
W.G. Ryan - MVP
You can use DataTable.Select to get just the matching rows for both A and B.
That should do it for you.
Show quote
"UJ" <f***@nowhere.com> wrote in message
news:ORgWf49DGHA.1384@TK2MSFTNGP11.phx.gbl...
>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.
>
>
Author
2 Jan 2006 10:07 PM
Elton W
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



Show quote
"UJ" wrote:

> 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.
>
>
>
Author
3 Jan 2006 9:41 AM
Cor Ligthert [MVP]
UJ,

That ToTable as Bill mention is one of those things that are great in
version 2005 however almost nowhere written. If you have versions before
that, than you can maybe use this sample.

http://www.vb-tips.com/default.aspx?ID=dcad9a66-1366-4d61-8d32-1a580eb893b2

Remove in that than the selection to make it distinct

I hope this helps,

Cor

AddThis Social Bookmark Button