Welcome to the Xceed Community | Help
Community Search  
More Search Options

How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

Sort Posts: Previous Next
  •  06-08-2012, 7:09 PM Post no. 32221

    How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    Hi, we have a requirement that when user expand or collapse a group on xceed datagrid, we need to persist the state so when the UI is refreshed, the group should remain the same state, expand or collapse. How do we achive that?

    Thanks!

  •  06-09-2012, 6:22 PM Post no. 32222 in reply to 32221

    Re: How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    Hi Olivia,

    The only way to do this is to program it yourself. Currently, when the data source is refreshed, the DataGrid will expand or collapse the groups depending on the InitiallyExpanded property. You will need to save the settings and reload them once the data source is refreshed. I had written this piece of code that will pay attention to all groups that have been expanded or collapse and reapplies them once the collection view has been refreshed. It is more of a hack, but it is the only way to get this feature working right now:

    C#
    ---------------
    DataGridCollectionView view = this.MyDGC.ItemsSource as DataGridCollectionView;
                if (view == null) { return; }

                //if there aren't any groups, just refresh
                if (view.Groups.Count < 1)
                {
                    view.Refresh();
                    return;
                }
                bool[] expandedGroups = new bool[view.Groups.Count];

                for (int i = 0; i < view.Groups.Count; i++)
                {
                    expandedGroupsIdea = this.MyDGC.IsGroupExpanded(view.GroupsIdea as CollectionViewGroup);
                }

                view.Refresh();
               
                for (int i = 0; i < view.Groups.Count; i++)
                {
                    if (!expandedGroupsIdea)
                        this.MyDGC.CollapseGroup(view.GroupsIdea as CollectionViewGroup);
                }
    ---------------


    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
  •  06-11-2012, 5:24 PM Post no. 32235 in reply to 32222

    Re: How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    This works great. Thanks Marc!
  •  06-12-2012, 1:34 PM Post no. 32241 in reply to 32235

    Re: How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    We are able to get the state of the groups by the suggested way but are having trouble to apply back. We apply the settings after the data is refreshed but it looks like DataGridControl is doing the group expanding itself and we need to know when this happens so we can apply the settings after that.
  •  06-12-2012, 10:39 PM Post no. 32243 in reply to 32241

    Re: How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    Hi Olivia,

    I'm not able to replicate this issue. My next question is how do you refresh the data? Are you calling the Refresh method on the DataGridCollectionView or are you re-binding the ItemsSource of the DataGridControl? If you need the event for when the data source gets refreshed (Refresh method), you will need to subscribe to the CollecionChanged event on the DataGridCollectionView. You must do this from the ItemsSourceChangedCompleted event first. For example:

    C#
    -----------
    private void MyDGC_ItemsSourceChangeCompleted(object sender, EventArgs e)
    {
        Debug.WriteLine("MyDGC_ItemsSourceChangeCompleted");
        ((this.MyDGC.ItemsSource as DataGridCollectionView) as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(MainWindow_CollectionChanged);
    }

    void MainWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Reset)
        {
            Debug.WriteLine("Called");
        }
    }
    -----------


    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
  •  06-14-2012, 6:21 PM Post no. 32252 in reply to 32243

    Re: How to persist the grouping state (collapsed or expanded) of a Xceed datagrid

    This works. Thanks again for all the help, Marc!
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.