Hi micah,
Refer below sample.
For this sample to work you will need to download the Microsoft Northwind database using the following link.
Download Northwind Database
CS.aspx
<div>
<asp:DataList ID="dlCustomers" runat="server" OnItemDataBound="dlCustomers_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>
Name
</th>
<th>
City
</th>
<th>
Order
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%#Eval("ContactName")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City")%>'></asp:Label>
</td>
<td>
<asp:DataList ID="dlOrders" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>
Order Id
</th>
<th>
Order Date
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblOrderId" Text='<%#Eval("OrderId")%>' runat="server" />
</td>
<td>
<asp:Label ID="lblDate" Text='<%#Convert.ToDateTime(Eval("OrderDate")).ToString("dd/MM/yyyy")%>'
runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" Visible="<%#bool.Parse((dlCustomers.Items.Count==0).ToString())%>"
Text="No Record Found!">
</asp:Label>
</table>
</FooterTemplate>
</asp:DataList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
CS.aspx.cs
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dlCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName,City FROM Customers");
dlCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].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;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dataRowView = e.Item.DataItem as DataRowView;
string customerId = dataRowView["CustomerID"].ToString();
DataList orderDataList = e.Item.FindControl("dlOrders") as DataList;
orderDataList.DataSource = GetData("SELECT TOP 5 OrderId,OrderDate FROM Orders WHERE CustomerID = '" + customerId + "'");
orderDataList.DataBind();
}
}
}
VB.aspx
<div>
<asp:DataList ID="dlCustomers" runat="server" OnItemDataBound="dlCustomers_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>
Name
</th>
<th>
City
</th>
<th>
Order
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%#Eval("ContactName")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City")%>'></asp:Label>
</td>
<td>
<asp:DataList ID="dlOrders" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>
Order Id
</th>
<th>
Order Date
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblOrderId" Text='<%#Eval("OrderId")%>' runat="server" />
</td>
<td>
<asp:Label ID="lblDate" Text='<%#Convert.ToDateTime(Eval("OrderDate")).ToString("dd/MM/yyyy")%>'
runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Visible="<%#Boolean.Parse(dlCustomers.Items.Count = 0)%>"
Text="No Record Found!">
</asp:Label>
</table>
</FooterTemplate>
</asp:DataList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
VB.aspx.vb
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
dlCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName,City FROM Customers")
dlCustomers.DataBind()
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub dlCustomers_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim dataRowView As DataRowView = TryCast(e.Item.DataItem, DataRowView)
Dim customerId As String = dataRowView("CustomerID").ToString()
Dim orderDataList As DataList = TryCast(e.Item.FindControl("dlOrders"), DataList)
orderDataList.DataSource = GetData((Convert.ToString("SELECT TOP 5 OrderId,OrderDate FROM Orders WHERE CustomerID = '") & customerId) + "'")
orderDataList.DataBind()
End If
End Sub
End Class
Screenshot