Copying Rows to a Datatable

B

byrd48

Hi,
am attempting to create a DataTable by copying a table from a data
set, then filter the rows as follows:

DataTable dt = DataSet1.Tables[0].Copy;

DataRow[] dr = dt.Select("myexpression");

dt.Rows.Clear();

dt.Rows.Add(dr);

I have validated that teh number of columns in the datatable matches
the number of items in the DataRow.ItemArray, but it still throws an
exception on the last line that the index of dr is greater than the
number of columns in the datatable.
Thanks in advance,

Jon
 
A

Alvin Bruney [MVP]

I think you are a bit confused, at least the code is confused. What you have
in code clones a datatable and filters it to a subset of rows. Then, source
dataset is cleared which also clears the target dataset because the
dt.select statement actually is a shallow copy/reference assignment and not
a true deep copy. Then you turn around and add the empty datarow objects
back into the cleared datatable. I'm certain that this is not what you want
to do. Otherwise, i'm confused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top