?
Solved

Duplicate a row

Posted on 2016-11-07
2
Medium Priority
?
80 Views
Last Modified: 2016-11-09
Hi,


When declare a datarow = datarow, when change value in one, the changes are reflected in other:


Dim row 1 As DataRow
Dim row2  As DataRow

row2=row1

row2("Id")=100

When put row2=row1and modify a column field the value is the same for row1 and row2. How can declare row2 to "clone" row 1, but when modify row2 only affects row2 and not row1?

best regards
0
Comment
Question by:rflorencio
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 41877683
Just to explain.  When you write
Dim row 1 As DataRow
Dim row2  As DataRow

row2=row1

the you have ONE row only! In other words, both row1 and row2 point to the SAME object.
So this will NOT work.

Please see the solution on MSDN:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a489c1d2-ffb5-4ef8-abe4-fa58731fee49/how-to-clone-datarow-?forum=csharpgeneral

===========================================================================

See my code below:
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("MyTable");
            DataColumn c1 = new DataColumn("c1", typeof(string));
            DataColumn c2 = new DataColumn("c2", typeof(string));
            dt.Columns.Add(c1);
            dt.Columns.Add(c2);
            DataRow dr1 = dt.NewRow();
            DataRow dr2 = dt.NewRow();
            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dr1[0] = "A";
            dr1[1] = "B";
            
            dr2.ItemArray = (object[])dr1.ItemArray.Clone();
            dr2[1] = "HELLO!";

            foreach (DataRow dr in dt.Rows)
            {
                foreach (object o in dr.ItemArray)
                {
                    Console.Write(o.ToString() + "\t");
                }
                Console.WriteLine();
            }

            Console.ReadLine();


        }
    }
}

Open in new window

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41877737
Does this work?
row2.ItemArray = (object[])row1.ItemArray.Clone()


Oops - not looked too closely at the previous comment.  I'd just seen the foreach and thought it copied each field.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question