Shai
Shai

Reputation: 569

Need to merge two or more datatable vb.net

I have 5 different DataTables. Each DataTable has always the same amount of rows with one repeated column called EmployeeID, which is primary key. Now I need to combine all columns from all tables along with data into one DataTable All DataTable are part of the same DataSet called Employee

DT1
EmployeeID, Name   , Age , Phone
1,          Mr. A  , 45  , 123456789

DT2
EmployeeID, Address      , Rank        , Title
1,          Main Street  , Top Level   , Manager

Expected output. 
The DTAll table will have the following columns with data
EmployeeID, Name   , Age,  phone     , Address     , Rank      , Title
1           Mr. A  , 45 ,  123456789 , Main Street , Top Level , Manager

Upvotes: 1

Views: 9677

Answers (1)

John Bustos
John Bustos

Reputation: 19544

If I'm understanding your question correctly, I would suggest you look into the DataTable.Marge() method - like here.

It would look like something along the following code:

    DT1.PrimaryKey = New DataColumn() {DT1.Columns("EmployeeID")}
    DT2.PrimaryKey = New DataColumn() {DT2.Columns("EmployeeID")}

    DT1.Merge(DT2)

Hope I understood correctly and that that helped!

Upvotes: 3

Related Questions