Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
persist expanded hierarchy state??
posted

My users are complaining about the expanded nodes of the hierarchical grid all resetting back to collapsed between page views...

Does any version of the web grid provide a way to persist the current state of the expanded hierarchy between page views ??  I am currently using  web grid version 6.1.20061.1055 with the .NET 2.0 framework. I can probably upgrade the web grid if a newer version can do this (however, I cannot upgrade .NET at this time).

Or maybe the version I am using now can do what I want and I'm just not seeing it ?? 

Thanks,
Jerry

 

Parents
No Data
Reply
  • 194
    posted

    I didn't see a way to make the grid do this itself but here's some code that can store and restore the expanded state of a heirarchical ultrawebgrid. It might be slightly convoluted with the internal class but because it has to store heirarchical information it seemed easiest to do it this way.

     If you can store the expanded state in the session before they navigate away then you can restore the expanded state on page load.

    private static void RestoreExpandedState(UltraWebGrid ultraWebGrid, List<RowIndex> _expandedRows)
    {
        RowsCollection rows = ultraWebGrid.Rows;
        RestoreExpandedStateRecursive(_expandedRows, rows);

    }

    private static void RestoreExpandedStateRecursive(List<RowIndex> rowIndexes, RowsCollection rows)
    {
        foreach (UltraGridRow row in rows)
        {
            RowIndex rowIndexFromList = GetRowIndexFromExpandedList(rowIndexes, row);

            if (rowIndexFromList != null)
            {
                row.Expand(false); if (row.Rows != null)
                {
                    RestoreExpandedStateRecursive(rowIndexFromList.ChildIndexes, row.Rows);

                }
            }
        }
    }

    private static void StoreExpandedState(UltraWebGrid ultraWebGrid, List<RowIndex> _expandedRows)
    {
        int level = 0;
        RowsCollection rows = ultraWebGrid.Rows;

        StoreExpandedStateRecursive(_expandedRows, level, rows);
    }

    private static void StoreExpandedStateRecursive(List<RowIndex> _expandedRows, int level, RowsCollection rows)
    {
        foreach (UltraGridRow row in rows)
        {
            if (row.Expanded)
            {
                List<RowIndex> childIndexes = new List<RowIndex>();
                StoreExpandedStateRecursive(childIndexes, level + 1, row.Rows);

                RowIndex rowIndex = new RowIndex(level, row.Index, childIndexes);
                _expandedRows.Add(rowIndex);

            }
        }
    }

    private static RowIndex GetRowIndexFromExpandedList(List<RowIndex> rowIndexes, UltraGridRow row)
    {
        return rowIndexes.Find(delegate(RowIndex rowIndex)
        {
            return row.Index == rowIndex.Index;
        });
    }

    class RowIndex
    {
        private int _Level;
        private int _Index;

        private List<RowIndex> _ChildIndexes;
        public RowIndex(int level, int index, List<RowIndex> childIndexes)
        {
            _Level = level;
            _Index = index;
            _ChildIndexes = childIndexes;
        }

        public int Index
        {
            get { return _Index; }
            set { _Index = value; }
        }

        public int Level
        {
            get { return _Level; }
            set { _Level = value; }
        }

        public List<RowIndex> ChildIndexes
        {
            get { return _ChildIndexes; }
            set { _ChildIndexes = value; }
        }
    }

     

    Edit: Fixed the formatting of the code.

Children
No Data