Solved

Adding data from one DataTable to another DataTable?

Posted on 2014-09-05
9
196 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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