Solved

Copying a DataRow from one DataTable to another DataTable

Posted on 2004-04-07
4
1,671 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
[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
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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