I have a gridview which has a default grouping (using a GroupDescriptor). The property AutoExpandGroups is set to true, so all groups are expanded by default.
This works fine and the user can easily collapse/expand the groups he wants to hide or show. However, the problem is that after a refresh of the itemssource (ie a NotifyPropertyChanged event in the ViewModel), all groups are automatically expanded again.
So I was wondering what the best way is to persist the expanded/collapse state of the groups when refreshing. The persistence should only last as long as the usercontrol is opened, there's no need to remember this for a next time.
Should I subscribe to the expand/collapse events of the groups and keep track of the changes so I can re-apply them on a refresh? Or is there an easier way?
Tia
8 Answers, 1 is accepted
Based on the sample for save & load gridview layout, you may try to expose a new class GroupRowSetting that exposes a property - IsExpanded. Thus, following up the pattern for saving the Column's settings for instance, you may do the same but targeting GridViewGroupRow.
Maya
the Telerik team

I can't seem to find a property for this in the radgridview. I tried several other ways, but without success:
- The GroupDescriptors and Grouping or Grouped events
- The RowLoaded event (doesn't fire for GridViewGroupRows).
- Recursively traversing the children in the visual tree, starting with the radgridview itself
Basically, you may get the GridViewGroupRow-s for the main grid using the ChildrenOfType<GridViewGroupRow> extension method. However, I am sending a sample project illustrating another - much easier approach for saving the collapsed/ extended state of the groups. All you need to do is to set the AutoExpandGroups to false before refreshing the ItemsSource.
Maya
the Telerik team

However, just setting AutoExpandGroups to false did the trick!
I did this in the DataLoaded event of the gridview, so the first time the groups are still auto expanded.
Thanks for your help!

We already answered your support ticket on the subject. For convenience we will paste our answer here as well:
Indeed the sample project does not implement what you are looking for, it is just the description my colleague gave. I will try to explain in details.
First of all you need to store all expanded groups - RadGridView.GroupRowIsExpanded is the perfect event for that. Indeed the code pasted below is not the "full" solution, since one group could be expanded and collapsed several times. You will need to handle all the possible cases.
public MainPage()
{
InitializeComponent();
this.radGridView.GroupRowIsExpandedChanged += new EventHandler<
Telerik.Windows.Controls.GridView.GroupRowEventArgs
>(radGridView_GroupRowIsExpandedChanged);
}
List<
IGroup
> expandedGroups = new List<
IGroup
>();
void radGridView_GroupRowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.GroupRowEventArgs e)
{
if (e.Row.IsExpanded)
{
this.expandedGroups.Add(e.Row.Group);
}
}
Then you should expand the saved groups one by one.
Kind regards,
Yoan
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.


I am handling it this way and its working. :)
private void mylist_DataLoaded(object sender, EventArgs e)
{
foreach (var item in expandedGroups)
{
mylist.ExpandGroup(item);
}
}
List<IGroup> expandedGroups = new List<IGroup>();
private void Risk_GroupRowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.GroupRowEventArgs e)
{
IGroup item = null;
if (e.Row.IsExpanded && !expandedGroups.Where(x => x.Key == e.Row.Group.Key).Any())
this.expandedGroups.Add(e.Row.Group);
else if (!e.Row.IsExpanded)
{
item = this.expandedGroups.Where(x => x.Key == e.Row.Group.Key).First();
if(item != null)
expandedGroups.Remove(item);
}
}