Solved

Adding data from one DataTable to another DataTable?

Posted on 2014-09-05
9
193 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

15 Experts available now in Live!

Get 1:1 Help Now