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
Then attach these two functions to your itemdatabound and databound events on the acutal grid itself
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!
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!