Background
Data often comes from multiple resources. Maybe sometimes it's a similar data type but sometimes the information is different. Consider a bus reservation system with a different vendor from which similar bus seats are reserved and those are various sources. If the bus owner wants to see a single result set then we need to merge the data into a single set. I am just giving a simple scenario, it also may be used for a different scenario.
So let us learn step-by-step how to merge multiple tables into a single table.
Step 1
Create an ASP.Net web application as in the following:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Provide the project a name such as "MergeMultipleDataTable" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then select the Default.aspx page.
- Drag and drop three Grid Views to bind the records after joining the two tables.
Now the Default.aspx source code will be as follows:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Defafult.aspx.cs" Inherits="MergeMultipleDataTable.Defafult" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body style="background-color: Blue">
- <h4 style="color: White">
- Article by Vithal Wadje</h4>
- <form id="form1" runat="server">
- <div>
- <h4 style="color: White">
- DataTable First Records Before Merging
- </h4>
- <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <EditRowStyle BackColor="#7C6F57" />
- <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#E3EAEB" />
- <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F8FAFA" />
- <SortedAscendingHeaderStyle BackColor="#246B61" />
- <SortedDescendingCellStyle BackColor="#D4DFE1" />
- <SortedDescendingHeaderStyle BackColor="#15524A" />
- </asp:GridView>
- <br />
- <h4 style="color: White">
- DataTable second Records Before Merging
- </h4>
- <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <EditRowStyle BackColor="#7C6F57" />
- <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#E3EAEB" />
- <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F8FAFA" />
- <SortedAscendingHeaderStyle BackColor="#246B61" />
- <SortedDescendingCellStyle BackColor="#D4DFE1" />
- <SortedDescendingHeaderStyle BackColor="#15524A" />
- </asp:GridView>
- <br />
- </div>
- <h4 style="color: White">
- DataTable second Records after Merging
- </h4>
- <asp:GridView ID="GridView3" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <EditRowStyle BackColor="#7C6F57" />
- <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#E3EAEB" />
- <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F8FAFA" />
- <SortedAscendingHeaderStyle BackColor="#246B61" />
- <SortedDescendingCellStyle BackColor="#D4DFE1" />
- <SortedDescendingHeaderStyle BackColor="#15524A" />
- </asp:GridView>
- <br />
- </form>
- </body>
- </html>
Step 2
Create the records for the table (you can also bind to records from the database).
Scenario
We have a different vendor from which we hire employees and they provide the employee records to our software development team using a web service and our job is to merge all the vendor records into one single data table so we can insert them into our database.
Open the Default.aspx.cs page and create a Vendor table (consider they are provided) as in the following:
- DataTable dt = new DataTable();
- DataRow dr = null;
- dt.Columns.Add("Id", typeof(int));
- dt.Columns[0].AutoIncrementSeed = 1;
- dt.Columns[0].AutoIncrement = true;
- dt.Columns.Add("Name");
- dt.Columns.Add("Employer");
- dr = dt.NewRow();
- dr["Name"] = "Vithal Wadje";
- dr["Employer"] = "LT";
- dt.Rows.Add(dr);
- DataRow dr2 = null;
- dr2 = dt.NewRow();
- dr2["Name"] = "Dinesh";
- dr2["Employer"] = "Microsoft";
- dt.Rows.Add(dr2);
The Vendor table's records will be as follows:
Now create the second vendor table (consider they are provided) as in the following:
- DataTable dt2 = new DataTable();
- DataRow dr1 = null;
- dt2.Columns.Add("Id", typeof(int));
- dt2.Columns[0].AutoIncrementSeed = 1;
- dt2.Columns[0].AutoIncrement = true;
- dt2.Columns.Add("Name");
- dt2.Columns.Add("Employer");
- dr1 = dt2.NewRow();
- dr1["Name"] = "Sudhir Wadje";
- dr1["Employer"] = "Goverment";
- dt2.Rows.Add(dr1);
Now the second vendor table records will be such as follows:
Now we have a two tables from two different vendors, now we want to merge these two table's records into one table, then just use the merge method of DataTable and the table as in the following:
- //merging first data table into second data table
- dt2.Merge(dt);
- dt2.AcceptChanges();
Now we have merged the two tables into a single table and now the records will look such as follows:
Now from the preceding example, it's clear that we can merge two tables into a single table. Now let us learn about some of the merge rules of DataTables.

Mohammad TaqiPosted Jan 30, 2018, 5:45 AM
If you want to merge more than 2 Datatable than check this http://techgulf.blogspot.com/2018/01/merge-multiple-datatable-into-single.html
Rock OnPosted Jun 17, 2017, 2:23 AM
How to add a column or two in case where all the other columns are same having same row data.
Vithal WadjePosted Aug 30, 2016, 10:41 AM
Thanks
Anand PsPosted Aug 30, 2016, 3:05 AM
Was very useful.. thanks..
Upendra Pratap ShahiPosted Jun 29, 2015, 6:11 AM
nice one...