I have a list "A" with a text field "category", I wrote a Visual Webpart to create a ListView or Gridview which group by "category" , How can I display inb the header just once the category ? and here is my code (ascx, ascx.cs) `

public void getInfo1()

      SPSite site = new SPSite(SPContext.Current.Site.Url);
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["List Report 2"];
            SPQuery Q = new SPQuery();
            Q.Query = "<GroupBy Collapse='FALSE'><FieldRef Name='htes' /></GroupBy><OrderBy><FieldRef Name='Title' Ascending='true' /></OrderBy>";

            Q.ViewFields = "<FieldRef Name='Title'/>" + "<FieldRef Name='htes'/>" + "<FieldRef Name='FileRef'/>";
            SPListItemCollection item = list.GetItems(Q);

            ListView1.DataSource = item.GetDataTable();


<asp:ListView ID="ListView1" runat="server" EnableModelValidation="True"  >

                    <asp:PlaceHolder runat="server" ID="groupPlaceHolder">




<%# DataBinder.Eval(Container.DataItem, "htes")%>
                        <a href='<%# DataBinder.Eval(Container.DataItem, "FileRef")%>'>
                                <p class="title">
                                    <%# DataBinder.Eval(Container.DataItem, "Title")%>




enter image description here

  • <listview.... <ItemTemplate><div><%# DataBinder.Eval(Container.DataItem, "htes")%></div><div><%# DataBinder.Eval(Container.DataItem, "Title")%></div> – BKCHedlia Jul 1 '14 at 10:14

If you have a list and group by is applied on Category. The resultant OOB SharePoint list view already shows the data in a manner described by you.

However if you really want to create a gridview of your own then you can achieve the task using ASP.NET Gridview/Repeater but in that case you need lot of customization in your code. You may go for some third party assembly like this. But my real suggestion would be using a client side library like Kendo UI, format the data as JSON using the server side code or fetch the data using default sharepoint web services

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.