Is there a way to maintain group collapse/expand status on sorting, etc?

Posted by: egor.kachiguine on 7 January 2019, 6:51 am EST

    • Post Options:
    • Link

    Posted 7 January 2019, 6:51 am EST

    Right now, when we sort by a column, or add another crolumn to the group panel, all grouped items expand. Is there a way to override this behavior?

    I think we can track the groups and their status and restore it after sorting, but I wanted to see if there was an easy flag I could set, or something along those lines.

  • Posted 8 January 2019, 1:02 am EST

    Hi,

    We are sorry to let you know that FlexGrid does not provide any property/method to specify such behavior directly. However, as you said that we can store the Expand states before and apply them after sorting, so this way we can achieve it. You can refer the attached sample (prj_ExpandCollapse.zip) for this implementation.

    Thanks,

    Basant.

    prj_ExpandCollapse.zip

  • Posted 14 January 2019, 11:50 am EST

    Thanks for that example - we can make that work for our needs.

  • Posted 4 June 2019, 8:07 am EST

    Hi, I wanted to revisit this post. The provided sample was a good start, but started showing some odd instabilities in scenarios with complex groupings, etc. I was unsatisfied by various methods around the web, so I expanded on your initial suggestion and made it a bit more robust when it comes to complicated groupings where simply tracking the parent is not enough to locate a group row.

    Maybe someone will find it useful.

    
    public class FlexGridGroupMaintainer // TODO: change this ridiculous name
        {
            readonly Dictionary<string, bool> _collapsed = new Dictionary<string, bool>();
    
            public C1FlexGrid Grid { get; set; }
    
            public void SaveExpandedRows()
            {
                _collapsed.Clear();
                var groupRows = GetRows();
                var groupNames = GetGroupNames(groupRows);
                groupRows.ForEach(r => _collapsed[groupNames[r.Group]] = r.IsCollapsed);
            }
    
            public void RestoreExpandedRows()
            {
                var groupRows = GetRows();
                var groupNames = GetGroupNames(groupRows);
                groupRows.ForEach(r => r.IsCollapsed = _collapsed.TryGetValue(groupNames[r.Group], out var isCollapsed)
                    ? isCollapsed
                    : r.IsCollapsed);
            }
    
            List<GroupRow> GetRows() => Grid.Rows.OfType<GroupRow>().ToList();
    
            private Dictionary<CollectionViewGroup, string> GetGroupNames(IEnumerable<GroupRow> rows)
            {
                var groupNames = new Dictionary<CollectionViewGroup, string>();
    
                void StoreName(string root, CollectionViewGroup group)
                {
                    var nRoot = root + @group.Name;
                    groupNames[group] = nRoot;
                    if (group.IsBottomLevel) return;
                    group.Items.OfType<CollectionViewGroup>().ForEach(gr => StoreName(nRoot, gr));
                }
    
                rows.Where(g => g.Level == 0).ForEach(r => StoreName("", r.Group));
                return groupNames;
            }
        }
    
    
  • Posted 5 June 2019, 8:46 pm EST

    Hi Egor,

    Good to know that provided sample could help you to achieve your objective and thank you for sharing the improvised logic for the same.

    Regards,

    Basant

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels