tags:

views:

3991

answers:

3

If I have 2 DataTables (dtOne and dtTwo) and I want to merge them and put them in another DataTable (dtAll). How can I do this in C#? I tried the Merge statement on the datatable, but this returns void. Does Merge preserve the data? For example, if I do:

 dtOne.Merge(dtTwo);

Does dtOne change or does dtTwo change and if either one changes, do the changes preserve?

I know I can't do this because Merge returns void, but I want to be able to store the Merger of both dtOne and dtTwo in dtAll:

//Will Not work, How do I do this
dtAll = dtOne.Merge(dtTwo);
+5  A: 

The Merge method takes the values from the second table and merges them in with the first table, so the first will now hold the values from both.

If you want to preserve both of the original tables, you could copy the original first, then merge:

dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);
Jeromy Irvine
+1  A: 
dtAll = dtOne.Copy();
dtAll.Merge(dtTwo,true);

The parameter TRUE preserve the changes.

For more details refer the below link. http://msdn.microsoft.com/en-us/library/wkk7s5zk.aspx

A: 

is it possible that after merging I can have only the values of second table in the first?