Note: This article was originally published in 2011. Some steps, commands, or software versions may have changed. Check the current .Net documentation for the latest information.

Prerequisites

Before you begin, make sure you have:

  • Visual Studio or .NET CLI installed
  • .NET Framework or .NET Core SDK
  • Basic C# programming knowledge

The grouping functionality of the Ajax control RadGrid from (http://www.telerik.com/ “Telerik”) is probably one of its most attractive features. However, many users might encounter themselves in a situation where you can take action on any given element and you wish to update the information being displayed on-screen to correspond to the action you’ve taken on the datasource. The big problem that occurs at this point is that on each ‘postback’ (you can do async calls too but the principle is the same) the groups that were collapsed and expanded all default back to their original condition which could result in a frustrated user having to re-collapse groups to get back to the view they had configured. In order to overcome this you need to save the grouping state before a databinding event and then restore it after the grid is databound. Below is a code snippet on how to achieve this:  

///

/// Page load function

///

///The parameter is not used.

///The parameter is not used.

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

// Set the session used to store the index of collapsed groupings to null as this is a new visit to the page.

Session = null;

}

this.RGV_Entity.DataBound += new EventHandler(RadGrid1_DataBound);

this.RGV_Entity.DataBinding += new EventHandler(RadGrid1_DataBinding);

}

///

/// This method adds logic on how to handle a databound (restore the grouping set on screen)

///

///The parameter is not used.

///The parameter is not used.

private void RadGrid1_DataBound(object sender, EventArgs e)

{

LoadGroupsExpandedState(this.RGV_Entity);

}

///

/// This method adds logic on how to handle a databinding (save the grouping set on screen)

///

///The parameter is not used.

///The parameter is not used.

private void RadGrid1_DataBinding(object sender, EventArgs e)

{

SaveGroupsExpandedState(this.RGV_Entity);

}

///

/// Saves the state of the groupping on screen

///

///The RadGrid that needs to preserve it’s grouping state

private void SaveGroupsExpandedState(RadGrid grid)

{

GridItem[] groupItems = grid.MasterTableView.GetItems(GridItemType.GroupHeader);

if (groupItems.Length > 0)

{

listCollapsedIndexes = new List();

foreach (GridItem item in groupItems)

{

if (!item.Expanded)

{

listCollapsedIndexes.Add(item.RowIndex);

}

}

Session = collapsedIndexes;

}

}

///

/// Restores the state of the groupping on screen

///

///The RadGrid that needs to be restored to its grouping state

private void LoadGroupsExpandedState(RadGrid grid)

{

listCollapsedIndexes= Session as List;

if (listCollapsedIndexes!= null)

{

foreach (GridItem item in grid.MasterTableView.GetItems(GridItemType.GroupHeader))

{

if (listCollapsedIndexes.Contains(item.RowIndex))

{

item.Expanded = false;

}

}

}

}

More information regarding this can be obtained at the Telerik site: http://www.telerik.com/community/forums/aspnet/grid/radgrid-group-expand-collapse-state-is-lost.aspx

(http://img.zemanta.com/zemified_e.png?x-id=337ea93d-5016-442b-b6fe-dbbc89292f68)](http://www.zemanta.com/?px “Enhanced by Zemanta”)

Summary

You’ve successfully learned telerik radgrid loses it’s state of expanded and contracted groups whenever you databind. If you run into any issues, double-check the prerequisites and ensure your .Net environment is properly configured.