Hi ramco,
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table class='table table-bordered table-hover' id='tblParticipant'>
<thead>
<tr>
<th class='nosort2'>ID</th>
<th>Name</th>
<th class="text-center nosort2">Action</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="ltrlNum" runat="server" Text="<%# Convert.ToString(Container.ItemIndex + 1) %>"></asp:Literal>
<asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("CustomerId") %>' />
</td>
<td><asp:Literal ID="ltrlContactName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Literal></td>
<td>
<asp:LinkButton ID="lnkNominee" runat="server" class="navbar-nav-link font-weight-semibold">
<span class="text-pink"><i class="icon-folder-upload mr-1"></i>Add Nominee</span></asp:LinkButton>
</td>
<td>
<table class='table table-columned' id='tblNominee'>
<asp:Repeater ID="rptOrders" runat="server">
<ItemTemplate>
<tr>
<td><asp:Literal ID="ltrlId" runat="server" Text='<%# Eval("OrderId") %>'></asp:Literal></td>
<td><input type="checkbox" id="chkOrder" runat="server" /></td>
<td><asp:Literal ID="ltrlFreight" runat="server" Text='<%# Convert.ToDecimal(Eval("Freight")).ToString("N2") %>'></asp:Literal></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<asp:Button ID="btnAdd" runat="server" Font-Bold="true" Text="Add" OnClick="Insert" class="btn btn-primary" />
<br /> <br />
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false" class="table table-bordered table-hover">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="OrderId" HeaderText="OrderId" />
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:N2}" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.HtmlControls
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptCustomers.DataSource = GetData("SELECT TOP 3 * FROM Customers");
rptCustomers.DataBind();
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
rptOrders.DataSource = GetData(string.Format("SELECT TOP 3 * FROM Orders WHERE CustomerId='{0}'", customerId));
rptOrders.DataBind();
}
}
protected void Insert(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("CustomerId"),
new DataColumn("Name"),
new DataColumn("OrderId"),
new DataColumn("Price",typeof(decimal)) });
foreach (RepeaterItem customer in rptCustomers.Items)
{
string id = (customer.FindControl("hfCustomerId") as HiddenField).Value;
string name = (customer.FindControl("ltrlContactName") as Literal).Text;
Repeater rptOrders = customer.FindControl("rptOrders") as Repeater;
foreach (RepeaterItem order in rptOrders.Items)
{
HtmlInputCheckBox chkOrder = order.FindControl("chkOrder") as HtmlInputCheckBox;
if (chkOrder.Checked)
{
string orderId = (order.FindControl("ltrlId") as Literal).Text;
string price = (order.FindControl("ltrlFreight") as Literal).Text;
dt.Rows.Add(id, name, orderId, price);
// Insert code goes here.
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
rptCustomers.DataSource = GetData("SELECT TOP 3 * FROM Customers")
rptCustomers.DataBind()
End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim customerId As String = (TryCast(e.Item.FindControl("hfCustomerId"), HiddenField)).Value
Dim rptOrders As Repeater = TryCast(e.Item.FindControl("rptOrders"), Repeater)
rptOrders.DataSource = GetData(String.Format("SELECT TOP 3 * FROM Orders WHERE CustomerId='{0}'", customerId))
rptOrders.DataBind()
End If
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("CustomerId"), New DataColumn("Name"), New DataColumn("OrderId"), New DataColumn("Price", GetType(Decimal))})
For Each customer As RepeaterItem In rptCustomers.Items
Dim id As String = (TryCast(customer.FindControl("hfCustomerId"), HiddenField)).Value
Dim name As String = (TryCast(customer.FindControl("ltrlContactName"), Literal)).Text
Dim rptOrders As Repeater = TryCast(customer.FindControl("rptOrders"), Repeater)
For Each order As RepeaterItem In rptOrders.Items
Dim chkOrder As HtmlInputCheckBox = TryCast(order.FindControl("chkOrder"), HtmlInputCheckBox)
If chkOrder.Checked Then
Dim orderId As String = (TryCast(order.FindControl("ltrlId"), Literal)).Text
Dim price As String = (TryCast(order.FindControl("ltrlFreight"), Literal)).Text
dt.Rows.Add(id, name, orderId, price)
End If
Next
Next
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = query
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot