locked
ListView Group By RRS feed

  • Question

  • User-909867351 posted

    Hi

    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 

    AlternatingItemTemplate

    GroupTemplate

    etc.

    Any help?

    thank you

    Saturday, August 18, 2018 3:42 PM

Answers

  • 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

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