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

Remember expanded/collapsed state of groups after refresh

8 Answers 1149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Licenses
Top achievements
Rank 1
Licenses asked on 29 Oct 2010, 09:00 AM
Hello,

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

Sort by
0
Maya
Telerik team
answered on 29 Oct 2010, 04:27 PM
Hello Sodi We,

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.

Sincerely yours,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Licenses
Top achievements
Rank 1
answered on 05 Nov 2010, 02:41 PM
And how do I access the GridViewGroupRows in a RadGridView?

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
0
Accepted
Maya
Telerik team
answered on 08 Nov 2010, 06:12 PM
Hi Sodi We,

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.
 

All the best,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Licenses
Top achievements
Rank 1
answered on 09 Nov 2010, 10:35 AM
ChildrenOfType<GridViewGroupRow> did not return any results. I use MVVM, so I do not (easily) know when a refresh is going to occur from the code behind of the View. So I called this in the DataLoaded event of the gridview, but maybe that is too soon?

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!
0
Christian
Top achievements
Rank 1
answered on 12 Feb 2013, 12:36 PM
I tried this with the current sl5 library, but all groups are collapsed when I click the button, regardsless of which groups where expanded before
0
Yoan
Telerik team
answered on 15 Feb 2013, 07:54 AM
Hi Christian,

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.

0
Sanyam
Top achievements
Rank 1
answered on 28 Oct 2015, 05:35 AM
So after adding/remove the expanded/collapsed  data to expanded groups list from that event. how can i assign back it to the main gridview itemssource(how do i use this list to help retaining the state )?
0
Sanyam
Top achievements
Rank 1
answered on 28 Oct 2015, 06:30 AM

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);
            }
        }

Tags
GridView
Asked by
Licenses
Top achievements
Rank 1
Answers by
Maya
Telerik team
Licenses
Top achievements
Rank 1
Christian
Top achievements
Rank 1
Yoan
Telerik team
Sanyam
Top achievements
Rank 1
Share this question
or