ListView Group By RRS feed

  • Question

  • User-909867351 posted


    I have one listview connected to sqldatasource with the following statment "Select * from perfumes group by marca"

    Now for each different marca I want the marca name. For example:

    Marca um

    perfume a

    perfume b


    Marca dois

    perfume f

    perfume g 


    I have on my listview 




    Any help?

    thank you

    Saturday, August 18, 2018 3:42 PM


  • 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">
                    <table class="table table-striped" id="TestTable">
                        <thead>  <tr>
                           <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
               <%-- 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">
                             <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>

    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();
                        //get distinct marca
                        perfumeList.DataSource = table.AsEnumerable().Select(row => new { marca = row["marca"] }).Distinct();
            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();

    The result.

    Best regards ,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 20, 2018 7:40 AM