Hi Amitabha,
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Please refer below sample.
HTML
<asp:DataGrid ID="dgvCustomers" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CustomerID" HeaderText="ID" />
<asp:BoundColumn DataField="ContactName" HeaderText="Name" />
<asp:BoundColumn DataField="Country" HeaderText="Country" />
</Columns>
</asp:DataGrid>
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDataGrid();
}
}
protected void Search(object sender, EventArgs e)
{
List<CustomerOrder> customers = new List<CustomerOrder>();
for (int i = 0; i < dgvCustomers.Items.Count; i++)
{
DataGridItem gdr = dgvCustomers.Items[i];
CheckBox chkSelect = ((CheckBox)(gdr.FindControl("chkRow")));
if (chkSelect.Checked)
{
customers.Add(new CustomerOrder
{
CustomerID = gdr.Cells[1].Text.Replace(" ", ""),
Name = gdr.Cells[2].Text.Replace(" ", ""),
Country = gdr.Cells[3].Text.Replace(" ", ""),
Orders = this.GetOrders(gdr.Cells[1].Text.Replace(" ", ""))
});
}
}
}
private void BindDataGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Top 5 * FROM Customers", con))
{
con.Open();
dgvCustomers.DataSource = cmd.ExecuteReader();
dgvCustomers.DataBind();
con.Close();
}
}
}
private List<Order> GetOrders(string id)
{
List<Order> orders = new List<Order>();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 OrderID,Freight FROM Orders WHERE CustomerID= @Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
orders.Add(new Order
{
OrderID = Convert.ToInt32(sdr["OrderID"]),
Freight = Convert.ToDecimal(sdr["Freight"])
});
}
con.Close();
}
}
return orders;
}
public class CustomerOrder
{
public string CustomerID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public decimal Freight { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDataGrid()
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim customers As List(Of CustomerOrder) = New List(Of CustomerOrder)()
For i As Integer = 0 To dgvCustomers.Items.Count - 1
Dim gdr As DataGridItem = dgvCustomers.Items(i)
Dim chkSelect As CheckBox = (CType((gdr.FindControl("chkRow")), CheckBox))
If chkSelect.Checked Then
customers.Add(New CustomerOrder With {
.CustomerID = gdr.Cells(1).Text.Replace(" ", ""),
.Name = gdr.Cells(2).Text.Replace(" ", ""),
.Country = gdr.Cells(3).Text.Replace(" ", ""),
.Orders = Me.GetOrders(gdr.Cells(1).Text.Replace(" ", ""))
})
End If
Next
End Sub
Private Sub BindDataGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT Top 5 * FROM Customers", con)
con.Open()
dgvCustomers.DataSource = cmd.ExecuteReader()
dgvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Private Function GetOrders(ByVal id As String) As List(Of Order)
Dim orders As List(Of Order) = New List(Of Order)()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 OrderID,Freight FROM Orders WHERE CustomerID= @Id", con)
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
orders.Add(New Order With {
.OrderID = Convert.ToInt32(sdr("OrderID")),
.Freight = Convert.ToDecimal(sdr("Freight"))
})
End While
con.Close()
End Using
End Using
Return orders
End Function
Public Class CustomerOrder
Public Property CustomerID As String
Public Property Name As String
Public Property Country As String
Public Property Orders As List(Of Order)
End Class
Public Class Order
Public Property OrderID As Integer
Public Property Freight As Decimal
End Class
Screenshot