Hi amar,
Check the below example.
HTML
<div align="center">
<asp:GridView runat="server" ID="GridView1" Caption="DataTable 1" />
<br />
<asp:GridView runat="server" ID="GridView2" Caption="DataTable 2" />
<br />
<asp:GridView runat="server" ID="GridView3" Caption="DataTable After Merge" />
</div>
Namespace
C#
using System.Data;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("Col1"), new DataColumn("Col2"), new DataColumn("Col3") });
dt1.Rows.Add("abc", "efb", "mno");
GridView1.DataSource = dt1;
GridView1.DataBind();
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[2] { new DataColumn("Test1"), new DataColumn("Test2") });
dt2.Rows.Add("Am", "M");
GridView2.DataSource = dt2;
GridView2.DataBind();
DataTable dt3 = dt1.Copy();
for (int column = 0; column < dt2.Columns.Count; column++)
{
dt3.Columns.Add(dt2.Columns[column].ColumnName);
}
for (int row = 0; row < dt3.Rows.Count; row++)
{
for (int column = 0; column < dt2.Columns.Count; column++)
{
dt3.Rows[row][dt2.Columns[column].ColumnName] = dt2.Rows[row][column];
}
}
GridView3.DataSource = dt3;
GridView3.DataBind();
}
Screenshot