In a DataGrid we can show the data of a table easily. But if we want to show the data of two tables in a DataGrid then how we will show the data. For showing the data of two table in a single DataGrid we have to used the DataSet merge property.
For showing the data of two table in a dataGrid make sure that the field name and field type of both table should be same.
The aspx code is:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Merge Two DataSet</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="70%" align="center" border="4">
<tr><td height="20px"></td></tr>
<tr><td align="center">
<asp:DataGrid ID="GridAllRecord" runat="server" HeaderStyle-BackColor="blue">
</asp:DataGrid>
<tr><td height="20px"></td></tr>
</td></tr>
</table>
</div>
</form>
</body>
</html>
The aspx.cs code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
public void binddata()
{
con=new SqlConnection("Data Source=MCN101; Initial Catalog=MergeTable; Uid=sa; pwd=");
da = new SqlDataAdapter("Select * from Table1", con);
da.Fill(ds1, "Record");
da = new SqlDataAdapter("select * from Table2", con);
da.Fill(ds2, "Record");
ds1.Merge(ds2);
GridAllRecord.DataSource = ds1;
GridAllRecord.DataBind();
}
}
The data of Table1 is:

Figure 1.
The data of Table2 is:

Figure 2.
When user will run the application then the window will look like this:

Figure 3.