Solved

Adding data from one DataTable to another DataTable?

Posted on 2014-09-05
9
194 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
  • 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
 

Author Comment

by:BlakeMcKenna
ID: 40306316
That's what I want. I specified that dt(1) is an accumulative table!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

14 Experts available now in Live!

Get 1:1 Help Now