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

Grouping Persist Expand Collapse State without ViewState

2 Answers 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 12 Mar 2014, 07:17 PM
The Example of keeping the expanded/collapse state of groups that Telerik provide uses the viewstate. If you have viewstate disabled, it causes a few kinks. Since I had to write this myself, I figured I'd share in case anyone needed it.

These two were in a class call telerikhelpers but you could put them anywyere
    /// <summary>
/// this loads the saved states of the group headers
/// (whether they were collapsed or expanded) you have to pass in the
/// grid you want to bind
/// </summary>
/// <param name="grid">the grid you want to bind</param>
public static void LoadGroupsExpandedState(RadGrid grid)
{
    var listCollapsedIndexes = HttpContext.Current.Session[grid.ID] as ArrayList;
 
    if (listCollapsedIndexes != null)
    {
        foreach (GridItem item in grid.MasterTableView.GetItems(GridItemType.GroupHeader))
        {
            if (listCollapsedIndexes.Contains(item.RowIndex))
            {
                item.Expanded = false;
            }
        }
    }
}//LoadGroupsExpandedState
 
/// <summary>
/// this function sets a session variable to remember which grouping items have been
/// collapsed or expanded
/// </summary>
/// <param name="grid"></param>
/// <param name="curItem"></param>
public static void SaveGroupsExpandedState(RadGrid grid, GridItem curItem)
{
    GridItem[] groupItems = grid.MasterTableView.GetItems(GridItemType.GroupHeader);
    if (groupItems.Length > 0)
    {
 
        var listCollapsedIndexes = new ArrayList();
 
        //if this first one is true, then we need to add it to the list
        //basically the state that is getting passed in is the old state
        //so if it's expanded = true, then it was expanded, and it's
        //getting collapsed
        if (curItem.Expanded)
        {
            listCollapsedIndexes.Add(curItem.RowIndex);
        }//if curItemExpanded
        else
        {
            //check if it's in the list
            if (listCollapsedIndexes.Contains(curItem.RowIndex))
            {
                listCollapsedIndexes.Remove(curItem.RowIndex);
            }//if listCollapse
        }//else
 
 
        foreach (GridItem item in groupItems)
        {
            if (!item.Expanded && item.RowIndex != curItem.RowIndex)
            {
                listCollapsedIndexes.Add(item.RowIndex);
            }
        }
        HttpContext.Current.Session[grid.ID] = listCollapsedIndexes;
    }
}//SaveGroupsExpandedState


Then attach these two functions to your itemdatabound and databound events on the acutal grid itself

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    //get the grid
    RadGrid grid = sender as RadGrid;
 
    //if this is expand or collapse, save the state
    if (e.CommandName == RadGrid.ExpandCollapseCommandName)
    {
        TelerikHelpers.SaveGroupsExpandedState(grid, e.Item);
    }
 
}//ItemCommand
 
/// <summary>
/// This method adds logic on how to handle a databound (restore the grouping set on screen)
/// </summary>
///The parameter is not used.
///The parameter is not used.
protected void RadGrid1_DataBound(object sender, EventArgs e)
{
    TelerikHelpers.LoadGroupsExpandedState(sender as RadGrid);
}

If you make the first two accessible globally, you can literally attach multiple grids on a page to the same databound and item command events and it will just work. Hope this helps someone else!

2 Answers, 1 is accepted

Sort by
0
Ben
Top achievements
Rank 1
answered on 12 Mar 2014, 07:21 PM
Edit* attach the itemcommand function to your itemcommand event, not itemdatabound
0
Konstantin Dikov
Telerik team
answered on 17 Mar 2014, 12:17 PM
Hello Ben,

Thank you for sharing your solution with the community.

Since we value all contributions to the community and we highly appreciate that you are sharing this solution here, you will find your Telerik Points updates.


Regards,
Konstantin Dikov
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Ben
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or