Solved

Copying a DataRow from one DataTable to another DataTable

Posted on 2004-04-07
4
1,654 Views
Last Modified: 2011-09-20
OK, I'm trying to copy a DataRow from one DataTable to another DataTable.  I throw the exception: "This row already belongs to another table."  I, of course, yell at the machine, "Please make a copy of that row for me!"

      DataTable goose = new DataTable("Goose");
      dtWK = csv.ParseCSV(sFilePathName);
      DataRow dr1 = goose.Rows[5];

      DataTable duck = new DataTable("Duck");
      DataRow dr2 = duck.NewRow();
      dr2 = dr1;
      duck.Rows.Add(dr2);

I can accomplish what I want by looping thru each DataRow.ItemArray, but there must be a copy constructor somewhere, I tried DataRow.Clone(); but that doesn't seem to apply to the whole array.

      DataRow dr2 = duck.NewRow();
      int i = 0;
      foreach (object o in dr1.ItemArray)
           dr2[i++] = o.ToString();
      duck.Rows.Add(dr2);

Thanx, Jim in Vermont
0
Comment
Question by:SparrowVideo
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 10778844
use this

DataTable goose = new DataTable("Goose");
     dtWK = csv.ParseCSV(sFilePathName);
     DataRow dr1 = goose.Rows[5];

     DataTable duck = new DataTable("Duck");
     duck.Rows.Add(dr1.ItemArray);

B..M
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10778892
As you might have guessed, this error message is because any datarow always "belongs" to a datatable, and cannot exist independently. So once added to a table, the same row cannot be added again to another table.

You can actually use the following stmt for what you want to do:

duck.Rows.Add(dr1.ItemArray)

HTH,
Karun.
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10778911
Ah, forget it... I'll never learn to refresh before posting...
0
 

Expert Comment

by:thulsey
ID: 11164759
I had same problem.  Thanks for mention of .ItemArray.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with WinAPI Signatures in VB.NET for SetWindowSubclass 2 35
Need a complete list of ASP.NET page load events 3 28
Syntax error 9 45
Replace &lt; with < 14 56
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

18 Experts available now in Live!

Get 1:1 Help Now