Solved

Adding data from one DataTable to another DataTable?

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

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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