1. Add SelectedIndexChanged event to First GridView.
2. In that grab the value using which you can populate second GridView.
3. Populate the second GridView.
HTML
<form id="form1" runat="server">
<asp:GridView ID="gvEmployees" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" ItemStyle-Width="100" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<u>
<asp:Label ID="lblEmployeeName" Visible="false" runat="server" />
</u>
<br />
<br />
<asp:GridView ID="gvOrders" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="Order Id" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="OrderDate" HeaderText="Order Date" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="ShippedDate" HeaderText="Shipped Date" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</form>
Namespaces
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployeesGrid();
}
}
private void BindEmployeesGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, FirstName, Country FROM Employees"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string employeeId = gvEmployees.SelectedRow.Cells[0].Text;
this.lblEmployeeName.Visible = true;
this.lblEmployeeName.Text = gvEmployees.SelectedRow.Cells[1].Text + " Order's details:";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT OrderId, OrderDate, ShippedDate FROM Orders WHERE EmployeeId = @EmployeeId", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@EmployeeId", employeeId);
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvOrders.DataSource = dt;
this.gvOrders.DataBind();
}
}
}
}
SQL
Install the Northwind and Pubs Sample Databases in SQL Server Express
Screenshot