This is a migrated thread and some comments may be shown as answers.

Rad Grid - Grouping - Default Collapsed

14 Answers 867 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 01 Sep 2008, 01:07 PM
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

14 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 01 Sep 2008, 02:35 PM
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.
0
Michael
Top achievements
Rank 1
answered on 01 Sep 2008, 03:28 PM
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
0
Michael
Top achievements
Rank 1
answered on 03 Sep 2008, 12:25 PM
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
0
Rosen
Telerik team
answered on 04 Sep 2008, 06:38 AM
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.
0
Derek Hunziker
Top achievements
Rank 1
answered on 30 Jan 2009, 08:03 PM
In case anyone is looking to collapse all groups, just do:

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

0
Jason Divis
Top achievements
Rank 1
answered on 23 Feb 2010, 09:06 PM
And for those of you using v3, there is a property within Layout->MasterTableView called GroupsDefaultExpanded that you can set to false.
0
Katie Arnott
Top achievements
Rank 1
answered on 06 Apr 2010, 04:07 PM
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


0
Kiara
Top achievements
Rank 1
answered on 07 Apr 2010, 01:13 PM
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
0
Katie Arnott
Top achievements
Rank 1
answered on 07 Apr 2010, 01:34 PM
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;
                }
            }



0
Vamsi Kamal
Top achievements
Rank 1
answered on 22 Aug 2012, 10:52 AM
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
0
Vamsi Kamal
Top achievements
Rank 1
answered on 22 Aug 2012, 10:52 AM
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
0
Preetham
Top achievements
Rank 2
answered on 22 Aug 2012, 11:14 AM
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.. :)
0
Eva
Top achievements
Rank 1
answered on 05 Sep 2014, 08:11 AM
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
0
wallafe
Top achievements
Rank 1
answered on 28 Feb 2019, 01:55 PM
Thank you Jason, I help you a lot.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Michael
Top achievements
Rank 1
Rosen
Telerik team
Derek Hunziker
Top achievements
Rank 1
Jason Divis
Top achievements
Rank 1
Katie Arnott
Top achievements
Rank 1
Kiara
Top achievements
Rank 1
Vamsi Kamal
Top achievements
Rank 1
Preetham
Top achievements
Rank 2
Eva
Top achievements
Rank 1
wallafe
Top achievements
Rank 1
Share this question
or