Cloning A DataRow

S

Scott M.

I need to copy an entire row of a typed DataSet so that I can place it into
a different instance of the same type of typed DataSet. I don't see a clone
or copy method on the DataRow. Do I have to create and populate the row
manually?

-Scott
 
C

Cor Ligthert[MVP]

Scott,

It depends if it is for the same datatable or that you want to put it in an
other datatable.

As you know is the datarow using the columns descriptions from the
datatable, that is the reason that you never can copy whatever datarow to
another table. (The datarow has even one property table, which tells to what
tables it belongs (get its columns from)).

Be aware that with strongly typed datasets the merge is an important method,
while it is in my idea with non typed ones mostly ignored.

Cor
 
S

Scott M.

Hi Cor,

As I said the row I want to copy will be placed into another typed-dataset
of the same type, so there will be no issuse with column mapping.

-Scott
 
C

Cor Ligthert[MVP]

Scott,

Whatever you copy, then it is always a shalow copy of the copied one.

By instance this code (version 2008 type VB)

\\\
Dim scottsSet As New Scott
Dim scottsTable = scottsSet.DataTable1
Dim firstRow = scottsTable.NewDataTable1Row
firstRow.DataColumn1 = "One"
firstRow.DataColumn2 = "two"
scottsTable.AddDataTable1Row(firstRow)

Dim corsSet As New Scott
corsSet.Merge(scottsTable)
///

Here the scottsSet and the corsSet are in fact completely the same and
reference to the same data. (a change in the corsSet is in fact the same as
in the scottsSet).

This is because you are in fact only working with references. To add values
to the datarows you have really to set the items to values.

I assume you knew this already however wanted it to be showed by somebody
else.

Cor
 
S

Scott M.

Thanks Cor, but this is not what I'm looking for.

I want to make a copy of the DataRow object (and thus the data in the
DataRow), so that the copy can exist in a separate DataTable than the first
one. I'm not talking about a second variable reference.


-Scott
 
C

Cor Ligthert[MVP]

Scott,

Then you have to copy the values and not the references in that.

You can simple do that by using a for each loop through the DataColumns, the
only problem is that you can use the datacolumn from the references table
and have for the other one the DataColumn.Name (The string).

Cor
 
S

Scott M.

Yes Cor, I know how to copy the individual values out of a row. I was
hoping to not have to do that though.

Thanks,

Scott
 
S

Scott M.

To follow up on that....

The problem that I'm facing is that this is a typed-dataset and some of the
row data that will get copied from one typed-dataset to another (of the same
type) is read-only. So copying the data out of the first row is no problem,
but manually setting that value in the second typed-dataset is where the
problem is because I can't set the value of the read-only column.
 
C

Cor Ligthert[MVP]

Scott,

As I understand you well then that are fields like auto identifiers (and
timestamps) or fields that have to be DBNull.Value.

I simply do that by skipping those by testing them in the loop.

Cor
 
W

Wen Yuan Wang [MSFT]

Thanks for Cor's prompt reply.

Hello Scoot,

Have you tried with DataTable.ImportRow API? This way could copy datarow
into another table (of the same schema), even though some filed are
read-only.
For example:

DataSet1 ds1 = new DataSet1();
DataSet1TableAdapters.Table_1TableAdapter tta = new
ConsoleApplication83.DataSet1TableAdapters.Table_1TableAdapter();
tta.Fill(ds1.Table_1);

DataSet1 ds2 = new DataSet1();
foreach (DataSet1.Table_1Row tr in ds1.Table_1.Rows)
ds2.Table_1.ImportRow(tr);

Hope this helps, please feel free to let us know if there is anything
unclear. We are glad to assist you.
Best regards,
Wen Yuan

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Scott M.

No the field in the second DataSet should have the same value as the field
in the first DataSet, not DBNull.

Thanks anyway, Cor.
 
W

Wen Yuan Wang [MSFT]

You are welcome, Scott.
Feel free to let us know if you face any further issue.. We are glad to
assist you.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
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