HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" OnCheckedChanged="SelectOrders" AutoPostBack="true"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ItemStyle-Width="30" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Orders">
<ItemTemplate>
<asp:Table ID="tblOrders" runat="server">
</asp:Table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB.Net
Namespaces
Imports System.Data.SqlClient
Imports System.Data
VB Code
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateCustomers()
End If
PopulateOrders()
End Sub
Private Sub PopulateOrders()
For Each row As GridViewRow In Me.GridView1.Rows
Dim customerId As String = row.Cells(1).Text
Dim table As Table = TryCast(row.FindControl("tblOrders"), Table)
Dim constr As String = ConfigurationManager.ConnectionStrings("Constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TOP 5 OrderId FROM Orders WHERE CustomerId = @CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", customerId)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim tdRow As New TableRow()
For i As Integer = 0 To sdr.FieldCount - 1
Dim cell As New TableCell()
cell.Text = sdr.GetValue(i).ToString()
tdRow.Cells.Add(cell)
Next
table.Rows.Add(tdRow)
End While
con.Close()
End Using
End Using
Next
End Sub
Protected Sub SelectOrders(sender As Object, e As EventArgs)
If TryCast(sender, CheckBox).Checked Then
Dim row As GridViewRow = TryCast(TryCast(sender, CheckBox).NamingContainer, GridViewRow)
Dim table As Table = TryCast(row.FindControl("tblOrders"), Table)
Dim message As String = String.Empty
For Each tableRow As TableRow In table.Rows
message += tableRow.Cells(0).Text + ","
Next
message = message.Substring(0, message.LastIndexOf(","))
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", (Convert.ToString("alert('") & message) + "')", True)
End If
End Sub
Private Sub PopulateCustomers()
Dim constr As String = ConfigurationManager.ConnectionStrings("Constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TOP 5 * FROM Customers", con)
Using da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Sub
C#.Net
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateCustomers();
}
PopulateOrders();
}
private void PopulateOrders()
{
foreach (GridViewRow row in this.GridView1.Rows)
{
string customerId = row.Cells[1].Text;
Table table = row.FindControl("tblOrders") as Table;
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 OrderId FROM Orders WHERE CustomerId = @CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", customerId);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
TableRow tdRow = new TableRow();
for (int i = 0; i < sdr.FieldCount; i++)
{
TableCell cell = new TableCell();
cell.Text = sdr.GetValue(i).ToString();
tdRow.Cells.Add(cell);
}
table.Rows.Add(tdRow);
}
con.Close();
}
}
}
}
protected void SelectOrders(object sender, EventArgs e)
{
if ((sender as CheckBox).Checked)
{
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
Table table = row.FindControl("tblOrders") as Table;
string message = string.Empty;
foreach (TableRow tableRow in table.Rows)
{
message += tableRow.Cells[0].Text + ",";
}
message = message.Substring(0, message.LastIndexOf(","));
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "')", true);
}
}
private void PopulateCustomers()
{
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
Screenshot