Solved

Duplicate a row

Posted on 2016-11-07
2
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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