In this article I will explain with an example, how to merge and display multiple Tables in single GridView in ASP.Net using C# and VB.Net.
The multiple Tables will be merged and displayed in single GridView with the help of JOINS in SQL Server.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with four BoundField columns.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="OrderId" HeaderText="Order Id" />
<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Merge and display multiple Tables in single GridView in ASP.Net
Inside the Page Load event of the page, first a SQL Query is generated which will be used to fetch data from the Customers, Orders and Employees tables of the Northwind database with the help of INNER JOIN.
Then the SQL Query is passed to the GetData function which internally executes the SQL Query and fetches the records into a DataTable.
Finally the returned DataTable is used to populate the GridView.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string sql = "SELECT TOP 10 c.CustomerId, c.ContactName, o.OrderId,";
sql += " (e.FirstName + ' ' + e.LastName) EmployeeName";
sql += " FROM Customers c INNER JOIN Orders o on c.CustomerId = o.CustomerId";
sql += " INNER JOIN Employees e ON e.EmployeeId = o.EmployeeId";
GridView1.DataSource = this.GetData(sql);
GridView1.DataBind();
}
}
private DataTable GetData(string sql)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Connection = con;
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim sql As String = "SELECT TOP 10 c.CustomerId, c.ContactName, o.OrderId,"
sql += " (e.FirstName + ' ' + e.LastName) EmployeeName"
sql += " FROM Customers c INNER JOIN Orders o on c.CustomerId = o.CustomerId"
sql += " INNER JOIN Employees e ON e.EmployeeId = o.EmployeeId"
GridView1.DataSource = Me.GetData(sql)
GridView1.DataBind()
End If
End Sub
Private Function GetData(sql As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sql)
Using sda As New SqlDataAdapter(cmd)
cmd.Connection = con
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot
Demo
Downloads