How to merge a subset of rows from one dataset to another

M

moondaddy

I need to merge/copy/import or what ever you want to call it, select rows
from a dataset to another. for example, say ds1 has 10 rows and I want to
get only the rows where the column 'category' has a value of 2, and merge
these rows into ds2. I tried doing something like this:

Dim drs() As dsWkT.tbStepsRow = CType(dsWTAll.tbSteps.Select("Category =
2"), dsWkT.tbStepsRow())

Which successfully created an array of rows. When I tried to merge this
into a dataset I got an error saying that these rows already belonged to
another dataset. Is there a way around this like copy this array to a new
array where they no longer belong to any dataset? Or make a clone or
something? Or is there an all together different way to do this?

Thanks.
 
K

Kevin Yu [MSFT]

Hi Moondaddy,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to merge DataRows from another
dataset that meet certain conditions. If there is any misunderstanding,
please feel free to let me know.

When you get the DataRows, you can try to use DataSet.Merge method to put
the rows into the destination dataset. Here is more information
DataSet.Merge(DataRow[] rows);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatasetclassmergetopic1.asp

However, DataSet.Merge will overwrite or discard a row, if it has the
primary key already exists in the destination DataSet. In this case, we
have to copy each DataRow with it's column values. Here is an example:

For Each dr As dsWkT.tbStepsRow In drs()
ds.Tables(0).Rows.Add(dr.ItemArray)
Next

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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

Top