Persisting collapsed/expanded state of groups in a RadGrid

When an autopostback occurs, the groups in my RadGrid all expand, even though I have collapsed several of them.  Is there a way to prevent this from happening?

Related Articles

... and you 'll find more on the NET Development Menu

A session variable is used to persist the state of the Expanded property for each group header item.  The outer test in the ItemDataBound routine only allows GridGroupHeaderItems to be controlled.   The session variable name is the UniqueID of the item in the grid.  The following code shows how to keep the groups collapsed/expanded after an autopostback.

 

 

Private Sub RadGrid2_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid2.ItemDataBound
 
     ' Save expanded state of the header item
     If TypeOf e.Item Is GridGroupHeaderItem Then
 
         If Session(e.Item.UniqueID) IsNot Nothing Then
             e.Item.Expanded = CBool(Session(e.Item.UniqueID))
         Else
             e.Item.Expanded = True
             Session(e.Item.UniqueID) = True
         End If
     End If
 
 End Sub
 
 Private Sub RadGrid2_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles RadGrid2.ItemCommand
 
     If e.CommandName = RadGrid.ExpandCollapseCommandName Then
         Session(e.Item.UniqueID) = Not e.Item.Expanded
     End If
 
 End Sub

 

The Else portion of the test in RadGrid2_ItemDataBound determines if the groups are expanded (True) or collapsed (False) when the form is initially displayed.

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences