Rad Grid - Grouping - Default Collapsed

14 posts, 0 answers
  1. Michael
    Michael avatar
    18 posts
    Member since:
    Oct 2007

    Posted 01 Sep 2008 Link to this post

    Hi,

    I am using the latest version of RadGrid for ASP.NET with Ajax with its grouping functionallity.

    Everything is working as expected except that I want some of the groups to be collapsed when the page loads.. this might be based on state the user last left the list or maybe some other logic, i cant take care of that bit but i need a way of telling the list that a group that it should start collapsed, i'd rather do this on the server side but can do it on the client side if needed.

    Thanks in advance.

    Mike
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 01 Sep 2008 Link to this post

    Hello Michael,

    Here is an example:
    <%@ Page Language="C#" %> 
     
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <script runat="server"
     
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            System.Collections.Generic.List<object> list = new System.Collections.Generic.List<object>(); 
            for (int i = 0; i < 10; i++) 
            { 
                list.Add(new { ID = iName = String.Format("Name{0}", (i < 5)? 1 : 2) }); 
            } 
            RadGrid1.DataSource = list
        } 
     
        protected void Page_PreRender(object sender, EventArgs e) 
        { 
            RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[1].Expanded = false
        } 
    </script> 
     
    <html xmlns="http://www.w3.org/1999/xhtml"
    <head runat="server"
        <title></title
    </head> 
    <body> 
        <form id="form1" runat="server"
        <div> 
            <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
            <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
                <MasterTableView> 
                    <GroupByExpressions> 
                        <telerik:GridGroupByExpression> 
                            <GroupByFields> 
                                <telerik:GridGroupByField FieldName="Name" /> 
                            </GroupByFields> 
                            <SelectFields> 
                                <telerik:GridGroupByField FieldName="Name" /> 
                            </SelectFields> 
                        </telerik:GridGroupByExpression> 
                    </GroupByExpressions> 
                </MasterTableView> 
            </telerik:RadGrid> 
        </div> 
        </form> 
    </body> 
    </html> 
     


    Greetings,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Michael
    Michael avatar
    18 posts
    Member since:
    Oct 2007

    Posted 01 Sep 2008 Link to this post

    Hi Vlad,

    Thanks for the snippet and quick reply.. I'll give it a try and let you know how it get on.

    In the mean time I have another problem with grouping that perhaps you can help me with.

    I have 2 levels of grouping on my grid using 2 GridGroupByExpression objects created in the Page_Load().  I also have an aggregate on one of the fields to be shown in the group footer.

    Individually the two groups work fine (adding one at a time and testing), however when I add them both together I get two problems.

    1) The top level expand/collapse button does not work properly, I can still see the entries of the group but it removes the expand/collapse buttons of the sub groups?  If I then press the expand button again it returns to normal but with all the level 2 groups collapsed (hope that makes sense?)

    2) The aggregate values (sum) for the first group are only calculating values for the last sub group in its group.

    For example, if I have 2 level 1 groups and each has 3 level 2 groups.. if i look at the totals for each of the 3 sub levels in the first group they are correct (i.e. 3), but the total top level group sum is the same as the last sub group (i.e. 3 instead of 9).

    I hope all of that makes sense?

    Mike
  5. Michael
    Michael avatar
    18 posts
    Member since:
    Oct 2007

    Posted 03 Sep 2008 Link to this post

    Hi Vlad,

    I have found work around to my second 2 questions there although I still think they may be problems, especially point 2.

    I have however found another possible issue with grouping and your first suggestion for having groups collapsed by default.

    For some reason if I trap the PreRender event the grid does not have any items.. this is a little strange as the data binding has already taken place, I have solved this by placing your suggestion in the OnDataBound event of the grid.

    But there seems to be a problem with the group footers when an entry is collapsed by default as it still shows the group footer.  If I then expand and collapse the group again it goes away.

    I have a feeling I might be able to work around this by trapping the OnItemCreated event and hiding the footer item if its parent is collapsed but its not ideal.

    Any suggestions?

    Thanks
  6. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 04 Sep 2008 Link to this post

    Hi Michael,

    Unfortunately I was unable to reproduce the described behavior with missing items in PreRender event. Can you please give us little more details about your grid declaration and  the way it is databound? Please note that when set inside DataBound event, this logic will not be executed on every postback, but only when the grid control is databound, therefore is may lead to inconsistent behavior.

    Greetings,
    Rosen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Derek Hunziker
    Derek Hunziker avatar
    62 posts
    Member since:
    Sep 2008

    Posted 30 Jan 2009 Link to this post

    In case anyone is looking to collapse all groups, just do:

    foreach (GridItem gi in yourRadGrid.MasterTableView.GetItems(GridItemType.GroupHeader)) 
         gi.Expanded = false

  8. Jason Divis
    Jason Divis avatar
    8 posts
    Member since:
    Feb 2010

    Posted 23 Feb 2010 Link to this post

    And for those of you using v3, there is a property within Layout->MasterTableView called GroupsDefaultExpanded that you can set to false.
  9. Katie Arnott
    Katie Arnott avatar
    25 posts
    Member since:
    Dec 2009

    Posted 06 Apr 2010 Link to this post

    what if you only want the 2nd groupby to be collasped? not all...

    foreach (GridItem gi in (source as RadGrid).MasterTableView.GetItems(GridItemType.GroupHeader))
                    {
                        gi.Expanded = false;
                    }

    does not work for me...

    Thank you,
    Katie


  10. Kiara
    Kiara avatar
    309 posts
    Member since:
    Jul 2006

    Posted 07 Apr 2010 Link to this post

    You can check the group index value and collapse the group if this value starts with 1 (indices start with 0 as we all know), i.e.:

    foreach (GridItem gi in (source as RadGrid).MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        GridGroupHeaderItem ghi = gi as GridGroupHeaderItem;
           if(ghi.GroupIndex.StartsWith("1"))
             ghi.Expanded = false;
    }

    Kiara
  11. Katie Arnott
    Katie Arnott avatar
    25 posts
    Member since:
    Dec 2009

    Posted 07 Apr 2010 Link to this post

    Thank you Kiara.  I found this worked as well,

    foreach (GridItem gi in (source as RadGrid).MasterTableView.GetItems(GridItemType.GroupHeader))
                {
                    if (gi.GroupIndex.Contains("_"))
                    {
                        gi.Expanded = false;
                    }
                }



  12. Vamsi Kamal
    Vamsi Kamal avatar
    18 posts
    Member since:
    May 2010

    Posted 22 Aug 2012 Link to this post

    Hi Rosen,

    Is it possible to enable grouping drag and drop without providing any default column in RadGrid?
    When the RadGrid is loaded first time I don't want any grouping, Based on my need I will drag and drop the columns to use group by facility. Is it possible in RadGrid, if yes could you plz give me a sample code.

    Many thanks in advance,
    Vamsi
  13. Vamsi Kamal
    Vamsi Kamal avatar
    18 posts
    Member since:
    May 2010

    Posted 22 Aug 2012 Link to this post

    Hi Rosen,

    Is it possible to enable grouping drag and drop without providing any default column in RadGrid?
    When the RadGrid is loaded first time I don't want any grouping, Based on my need I will drag and drop the columns to use group by facility. Is it possible in RadGrid, if yes could you plz give me a sample code.

    Many thanks in advance,
    Vamsi
  14. Preetham
    Preetham avatar
    17 posts
    Member since:
    Sep 2012

    Posted 22 Aug 2012 Link to this post

    Thanks for the code snippet Jason Divis and Derek Hunziker, This is going to be a lot of help for my case.. will try it out.. :)
  15. Eva
    Eva avatar
    19 posts
    Member since:
    May 2012

    Posted 05 Sep 2014 in reply to Michael Link to this post

    Dear Michael,
    i had a similar problem with you with GroupFooters:
    "But there seems to be a problem with the group footers when an entry is collapsed by default as it still shows the group footer.  If I then expand and collapse the group again it goes away."

    and i managed to solved it with the above code


    Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
    For Each gi As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
    If gi.Expanded = True Then
    gi.Expanded = False
    End If
    Dim children As GridItem() = gi.GetChildItems()
    For Each child As GridItem In children
    If child.Expanded = True Then
    child.Expanded = False
    End If
    If child.ItemType = GridItemType.GroupFooter Then child.Visible = False
    Next
    'gi.Expanded = False
    Next

    the trick was this:
    If child.ItemType = GridItemType.GroupFooter Then child.Visible = False


    i think this will be helpful for others.

    End Sub
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017