0

I need to merge two datatable into one common datatable in version C# 3.5 version. I tried merge operation like dt1.merge(dt2). But it is appending datatable2 values into bottom of datatable1. But I need like below format.

DataTable1
Column1 Column2 Column3
  1        Row1      Row2
  2        Row3      Row4

DataTable2
Column4 Column5 Column6
  1        Row5      Row6
  2        Row7      Row8

Resultant Table should be like

DataTable1
Column1   Column2 Column3    Column5  Column6
  1        Row1      Row2   Row5    Row6
  2        Row3      Row4   Row7    Row8

any help would be appreciated

Dillibabu
  • 13
  • 1
  • 2
  • 5
  • Show us what you've got so far – Simon Belanger Jul 13 '13 at 10:41
  • You have got your answer here http://stackoverflow.com/questions/12628020/merging-2-datatables-in-to-1-datatable-with-same-number-of-rows – Ehsan Jul 13 '13 at 10:45
  • Looks like what you want is in an inner join, possible answer http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp – crafty Jul 13 '13 at 11:02

1 Answers1

0

Merge() will merge the data table vertically. You can iterate through first DataTable and append each column to second DataTable. In the following example tb2 will have all the columns of tb.

DataTable tb = new DataTable();
DataTable tb2 = new DataTable();

foreach (DataColumn item in tb.Columns)
{
  tb2.Columns.Add(item);
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65