Click here to Skip to main content
14,697,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have tried to Merge two DataTable objects with the method Merge and it seems that it behaves this way:
A = |col1|col2|
    |a1  |b1  |
    |a2  |b2  |
    |a3  |b3  |
    |a4  |b4  |

B = |col3|col4|
    |c1  |d1  |
    |c2  |d2  |
    |c3  |d3  |
    |c4  |d4  |

A.Merge(B) gives the following result:

|a1  |b1  |    |    |
|a2  |b2  |    |    |
|a3  |b3  |    |    |
|a4  |b4  |    |    |
|    |    |c1  |d1  |
|    |    |c2  |d2  |
|    |    |c3  |d3  |
|    |    |c4  |d4  |

What I want to have is:
|a1  |b1  |c1  |d1  |
|a2  |b2  |c2  |d2  |
|a3  |b3  |c3  |d3  |
|a4  |b4  |c4  |d4  |

In my researches, I could find different solutions such as creating empty columns, browsing rows and adding them one by one but first, this is cannot be a good way in term of performance.

Can any one help with this?
Updated 11-Oct-13 1:55am
Mike Meinz 11-Oct-13 8:07am
Your desired result shows a join not a merge. In your example, there does not appear to be a "key" that links rows in table A to a corresponding row in table B. You need a common "key" to create a result set as shown in your example.
Mehdi_S 11-Oct-13 9:07am
Yeah, I have explored that solution but what if I don't need a key? I just need to add columns even if I will end up missing rows?
Mike Meinz 11-Oct-13 9:55am
See Solution 1.
Get one row from Table A (If no rows left, put NULL into variables).
Get one row from Table B (If no rows left, put NULL into variables).
Add new row to Table C using variables.
Repeat until done with both Table A and Table B.
Mehdi_S 11-Oct-13 10:09am
yeah but imagine you have like 1000 rows and 300 columns to merge or bigger. the lopp treating one by one would consume time
Member 10266297 11-Oct-13 10:15am
look my dear you have to start with the following way and when you find better way move to it but till now you don't have a way to implement so don't wast you time implement the below and search for better..
Mike Meinz 11-Oct-13 11:20am
Of course, it will take time. If you don't want to do it this way, redesign the application so it does not have to be done.
Mehdi_S 14-Oct-13 9:08am
Well, that's what I did. but I'm posting this question to have better ideas :)

create new datatable that is made up of the four columns then insert the rows from both old is so easy like this way:

DataTable dt = new DataTable("ab");

DataRow dr = dt.NewRow();
dr["col1"] = "first value in col1 from table A"
dr["col2"] = "first value in col2  from table A"
dr["col3"] = "first value in col3 from table B"
dr["col4"] = "first value in col4 from table B"


for sure you have to add the using loops not like that but i am giving you an example
visit here..

Merging Cells in table[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900