Solved

Adding data from one DataTable to another DataTable?

Posted on 2014-09-05
9
198 Views
Last Modified: 2014-09-05
I have 2 DataTables. dt(0) gets data added to it after a "run" and then gets cleared out. But, before it gets cleared out, it takes all the data and adds it to another dt(1) via a For Loop.

My question? Is there an easier way to add this data to dt(1) rather than performing a Loop? dt(1) needs to be an accumulative table where dt(0) is just a temporary table.

This is how I'm currently doing it:

                    Dim newRow As DataRow

                    For Each row In table1.Rows
                        newRow = table2.NewRow

                        newRow("runNO") = row("runNO")
                        newRow("loadingPoint") = row("loadingPoint")
                        newRow("loadValue") = row("loadValue")
                        newRow("output") = row("output")
                        newRow("nonLinearity") = row("nonLinearity")
                        newRow("hysteresis") = row("hysteresis")
                        newRow("forceError") = row("forceError")
                        newRow("printOnCert") = CInt(cboPrintOnCert.Checked)

                        table2.Rows.Add(newRow)
                    Next

Open in new window


Thanks!
0
Comment
Question by:BlakeMcKenna
[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
  • 4
  • 4
9 Comments
 
LVL 27

Expert Comment

by:Sammy
ID: 40306229
Use the Clone method to copy the existing data table to a new one without using any type of loop

tblNewCopy = tblProducts.Clone()
0
 

Author Comment

by:BlakeMcKenna
ID: 40306246
Does this add the data in dt(1) to dt(2)?

I actually just found something:

dt(2).Merge(dt(1), True)
0
 
LVL 27

Expert Comment

by:Sammy
ID: 40306260
Merge is used to Merge two data tables with existing data between them.
Clone clones one data table to the other
In this case dt(1) will have all data from dt(0)
dt(1)=dt(0).Clone()
0
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!

 

Author Comment

by:BlakeMcKenna
ID: 40306316
That's what I want. I specified that dt(1) is an accumulative table!
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 40306333
Then use the Merge function. Just understand the meaning of the preserveChanges parameters (what you set to True)
from MSDN http://msdn.microsoft.com/en-us/library/wkk7s5zk(v=vs.110).aspx
The Merge method is typically called at the end of a series of procedures that involve validating changes, reconciling errors, updating the data source with the changes, and finally refreshing the existing DataTable.
When performing a merge, changes made to the existing data before the merge are preserved during the merge operation unless the developer specifies false for the preserveChanges parameter. If the preserveChanges parameter is set to true, incoming values do not overwrite existing values in the Current row version of the existing row. If the preserveChanges parameter is set to false, incoming values do overwrite the existing values in the Current row version of the existing row. For more information about row versions, see Row States and Row Versions.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40306347
Hi BlakeMcKenna;

Both Copy and Clone return a new DataTable. If you want to add the rows from dt(1) to dt(2) then use the Merge method as you stated.

dt(2).Merge(dt(1), True)
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40306368
Thank you!
0
 
LVL 27

Expert Comment

by:Sammy
ID: 40306370
They do but they have they return different "copy"
Clone creates a new DataTable with same structure and constraints but without data and Copy does same but includes data too.
0
 

Author Comment

by:BlakeMcKenna
ID: 40306445
Merge is what I need because I need to add data to the existing table that may or may not contain data...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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