Home
> ASP.Net, Csharp, DotNet > How to merge two data tables in ASP.Net/C# OR Merge 2 DataTables and store in a new one in ASP.Net/C#
How to merge two data tables in ASP.Net/C# OR Merge 2 DataTables and store in a new one in ASP.Net/C#
Merge multiple data-tables data into a another data-table using C#/ASP.Net
Here we are going to demonstrate a sample application which shows merge two data tables in ASP.Net. In this sample we are having two data tables with some data and merge data of each tables data. We binds first data table to a data grid second one to another grid and third data grid binds with merged data table.

Merge data tables in C#/ASP.Net
ASPX Page for Example of merge two datatables in C#/ASP.Net
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MergeDataTable.aspx.cs" Inherits="ExperimentLab.MergeDataTable" %> <!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> <form id="form1" runat="server"> <div> First DataTable <asp:GridView ID="gdDataTableOne" runat="server" /><br /> Second DataTable <asp:GridView ID="gdDataTableTwo" runat="server" /><br /> Merged DattaTable <asp:GridView ID="gdDataTableMergedData" runat="server" /><br /> </div> </form> </body> </html> -*******
CODE BEHIND Page for Merge datatable in C#/ASP.Net
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace ExperimentLab { public partial class MergeDataTable : System.Web.UI.Page { DataTable dtOne = new DataTable(); DataTable dtTwo = new DataTable(); protected void Page_Load(object sender, EventArgs e) { fillDataTableOne(); fillDataTableTwo(); mergeDataTables(); } private void mergeDataTables() { dtOne.Merge(dtTwo, false); gdDataTableMergedData.DataSource = dtOne; gdDataTableMergedData.DataBind(); } private void fillDataTableOne() { dtOne.Columns.Add("EmpID", typeof(Int32)); dtOne.Columns.Add("EmployeeName", typeof(string)); dtOne.Columns.Add("Department", typeof(string)); dtOne.Columns.Add("City", typeof(string)); DataRow dtOnerow = dtOne.NewRow(); // Create New Row dtOnerow["EmpID"] = 1; //Bind Data to Columns dtOnerow["EmployeeName"] = "Ram"; dtOnerow["Department"] = "Admin"; dtOnerow["City"] = "Kochi"; dtOne.Rows.Add(dtOnerow); dtOnerow = dtOne.NewRow(); // Create New Row dtOnerow["EmpID"] = 2; //Bind Data to Columns dtOnerow["EmployeeName"] = "Mahesh"; dtOnerow["Department"] = "Finance"; dtOnerow["City"] = "Delhi"; dtOne.Rows.Add(dtOnerow); dtOnerow = dtOne.NewRow(); // Create New Row dtOnerow["EmpID"] = 3; //Bind Data to Columns dtOnerow["EmployeeName"] = "Raj"; dtOnerow["Department"] = "Admin"; dtOnerow["City"] = "Kolkata"; dtOne.Rows.Add(dtOnerow); dtOnerow = dtOne.NewRow(); // Create New Row dtOnerow["EmpID"] = 4; //Bind Data to Columns dtOnerow["EmployeeName"] = "Virbal"; dtOnerow["Department"] = "HR"; dtOnerow["City"] = "Mumbai"; dtOne.Rows.Add(dtOnerow); gdDataTableOne.DataSource = dtOne; gdDataTableOne.DataBind(); } private void fillDataTableTwo() { dtTwo.Columns.Add("EmpID", typeof(Int32)); dtTwo.Columns.Add("EmployeeName", typeof(string)); dtTwo.Columns.Add("Department", typeof(string)); dtTwo.Columns.Add("City", typeof(string)); DataRow dtTworow = dtTwo.NewRow(); // Create New Row dtTworow["EmpID"] = 5; //Bind Data to Columns dtTworow["EmployeeName"] = "John"; dtTworow["Department"] = "Delivery"; dtTworow["City"] = "Goa"; dtTwo.Rows.Add(dtTworow); dtTworow = dtTwo.NewRow(); // Create New Row dtTworow["EmpID"] = 6; //Bind Data to Columns dtTworow["EmployeeName"] = "Jacob"; dtTworow["Department"] = "Finance"; dtTworow["City"] = "Kolkata"; dtTwo.Rows.Add(dtTworow); dtTworow = dtTwo.NewRow(); // Create New Row dtTworow["EmpID"] = 7; //Bind Data to Columns dtTworow["EmployeeName"] = "Srini"; dtTworow["Department"] = "HR"; dtTworow["City"] = "Jaipur"; dtTwo.Rows.Add(dtTworow); dtTworow = dtTwo.NewRow(); // Create New Row dtTworow["EmpID"] = 8; //Bind Data to Columns dtTworow["EmployeeName"] = "Gopal"; dtTworow["Department"] = "InfraStructure"; dtTworow["City"] = "Chennai"; dtTwo.Rows.Add(dtTworow); gdDataTableTwo.DataSource = dtTwo; gdDataTableTwo.DataBind(); } } }
Comments (0)
Trackbacks (0)
Leave a comment
Trackback
Recent Comments