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