?
Solved

Duplicate a row

Posted on 2016-11-07
2
Medium Priority
?
67 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 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

764 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