Solved

Duplicate a row

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now