Solved

Duplicate a row

Posted on 2016-11-07
2
53 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 29

Accepted Solution

by:
anarki_jimbel earned 500 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 44

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Initial get page response times and IIS8 15 51
2 questions 10 25
C# Gridview 1 33
Older JQuery functionality is not in the newer JQuery 1.4.4 versions, 6 27
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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