Please refer below code.
HTML
<div>
<asp:ListView ID="ListView1" runat="server" GroupItemCount="3" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="DataBound" DataKeyNames="CustomerId">
<LayoutTemplate>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px;
text-align: center">
<tr>
<td>
<b><u><span class="name">
<%# Eval("ContactName") %></span></u></b>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</td>
</ItemTemplate>
</asp:ListView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "SELECT top 10 CustomerId,ContactName FROM Customers";
ListView1.DataSource = this.GetData(query);
ListView1.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void DataBound(object sender, ListViewItemEventArgs e)
{
int i = 0;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
string customerId = ListView1.DataKeys[i].Value.ToString();
string query = "SELECT * FROM Customers WHERE CustomerId = '" + customerId + "';";
GridView gvDetails = (e.Item.FindControl("gvDetails") as GridView);
gvDetails.DataSource = GetData(query);
gvDetails.DataBind();
i++;
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim query As String = "SELECT top 10 CustomerId,ContactName FROM Customers"
ListView1.DataSource = Me.GetData(query)
ListView1.DataBind()
End If
End Sub
Private Function GetData(query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds)
Return ds
End Using
End Using
End Using
End Function
Protected Sub DataBound(sender As Object, e As ListViewItemEventArgs)
Dim i As Integer = 0
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim customerId As String = ListView1.DataKeys(i).Value.ToString()
Dim query As String = (Convert.ToString("SELECT * FROM Customers WHERE CustomerId = '") & customerId) + "';"
Dim gvDetails As GridView = TryCast(e.Item.FindControl("gvDetails"), GridView)
gvDetails.DataSource = GetData(query)
gvDetails.DataBind()
i += 1
End If
End Sub
Screenshot
