Reputation: 14565
I have 2 DataTables DT1
and DT2
suppose that each table has only one row.
how can i join the 2 datatables into one table DT3
?
as an example: DT1
has 2 columns FirstN
,LastN
DT2
has 2 columns salary
, currency
Therefore DT3
will have FirstN
,LastN
, salary
, currency
Upvotes: 0
Views: 2600
Reputation: 13106
You can achieve this using DataRelations, here's an example:
you can find lots of examples like this, just ask google ;)
Upvotes: 0
Reputation: 10366
Can you try this out and see if it works for you ? This is not exactly the intended use case for Merge
but i think it just might work in this case.
DT1.Merge (DT2, true, MissingSchemaAction.Add )
Note: I have not done this ever. But i think based on how Merge
is implemented, you may be able to get away with doing this.
From MSDN When the Merge method is called, the schemas of the two DataTable objects are compared, because it is possible that the schemas may have been changed. If the source DataTable contains schema elements (added DataColumn objects) that are missing in the target, the schema elements can be added to the target by setting the missingSchemaAction argument to MissingSchemaAction.Add. In that case, the merged DataTable contains the added schema and data.
Upvotes: 1
Reputation: 1501
First get all the columns from dt1 and dt2 and add them in dt3, after adding columns in dt3 start inserting the value using for loop on dt1 and dt2 so that you get each row from these datatable and by using the name of columns which are already in these datatable you get the specific value.
In this link you get how to insert the value in datatable
http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx
Mark my answer and point me if you get solution from my answer.
Upvotes: 0