Remember expanded/collapsed state of groups after refresh

9 posts, 1 answers
  1. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 29 Oct 2010 Link to this post

    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
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Oct 2010 Link to this post

    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
  3. DevCraft banner
  4. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 05 Nov 2010 Link to this post

    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
  5. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 08 Nov 2010 Link to this post

    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
  6. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 09 Nov 2010 Link to this post

    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!
  7. Christian
    Christian avatar
    52 posts
    Member since:
    Apr 2011

    Posted 12 Feb 2013 Link to this post

    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
  8. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 15 Feb 2013 Link to this post

    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.

  9. Sanyam
    Sanyam avatar
    2 posts
    Member since:
    Oct 2015

    Posted 28 Oct 2015 in reply to Yoan Link to this post

    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 )?
  10. Sanyam
    Sanyam avatar
    2 posts
    Member since:
    Oct 2015

    Posted 28 Oct 2015 Link to this post

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

Back to Top
DevCraft banner