User-893317190 posted
Hi mariolopes,
You could nest listviews in a listview and use the nested listviews to show your perfumes in the OnItemDataBound event of parent listview.
Below is my code.
My perfume has three columns :name,price and marca.
<form id="form1" runat="server">
<asp:ListView ID="perfumeList" runat="server"
ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="perfumeList_ItemDataBound">
<LayoutTemplate>
<table class="table table-striped" id="TestTable">
<thead> <tr>
<th>name</th>
<th>price</th>
</tr></thead>
<tbody>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tbody>
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<%-- table row to show marca --%>
<tr class="bg-primary"><td colspan="3" class="text-center"> <asp:Label ID="Label1" runat="server" Text='<%# Eval("marca") %>'></asp:Label> </td> </tr>
<%-- listview to show perfums with the same marca --%>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr><td><asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
<td><asp:Label ID="Label2" runat="server" Text='<%# Eval("price") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
</form>
My code behind.
private static string constr = ConfigurationManager.ConnectionStrings["EntityExe"].ConnectionString;
private DataTable table;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// get all the perfumes from database
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from perfume", constr))
{
table = new DataTable();
adapter.Fill(table);
//get distinct marca
perfumeList.DataSource = table.AsEnumerable().Select(row => new { marca = row["marca"] }).Distinct();
perfumeList.DataBind();
}
}
}
protected void perfumeList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//find the nested listview
System.Web.UI.WebControls.ListView listView= e.Item.FindControl("ListView1") as System.Web.UI.WebControls.ListView;
// find the name of current marca
Label label= e.Item.FindControl("Label1") as Label;
//get all the perfumes with the current marca
//here I use linq , you could use other ways only if you could get the perfumes having the current marca
listView.DataSource = table.AsEnumerable().Where(row => row["marca"] as string == label.Text).Select(row=>new {name=row["name"],price=row["price"] }).ToList();
listView.DataBind();
}
The result.

Best regards ,
Ackerly Xu