Skip to main content
All docs
V23.2

How to: Save and Restore the Expanded State of Nodes

  • 4 minutes to read

This example demonstrates how to save and load the expanded state of nodes, the focused node’s position, and selected nodes.

Play the animation below to see the result.

Save and Restore the State of Nodes - WinForms TreeList | DevExpress

Implement the TreeListViewState class

The TreeListViewState class implements the SaveState and LoadState methods. These methods allow you to persist and restore the state of nodes.

using System.Collections;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;

public class TreeListViewState {
    private ArrayList expanded;
    private ArrayList selected;
    private object focused;
    private object topNode;

    public TreeListViewState() : this(null) { }
    public TreeListViewState(TreeList treeList) {
        this.treeList = treeList;
        expanded = new ArrayList();
        selected = new ArrayList();
    }

    public void Clear() {
        expanded.Clear();
        selected.Clear();
        focused = null;
        topNode = null;
    }
    private ArrayList GetExpanded() {
        OperationSaveExpanded op = new OperationSaveExpanded();
        TreeList.NodesIterator.DoOperation(op);
        return op.Nodes;
    }
    private ArrayList GetSelected() {
        ArrayList al = new ArrayList();
        foreach(TreeListNode node in TreeList.Selection)
            al.Add(node.GetValue(TreeList.KeyFieldName));
        return al;
    }

    public void LoadState() {
        TreeList.BeginUpdate();
        try {
            TreeList.CollapseAll();
            TreeListNode node;
            foreach(object key in expanded) {
                node = TreeList.FindNodeByKeyID(key);
                if(node != null)
                    node.Expanded = true;
            }
            TreeList.FocusedNode = TreeList.FindNodeByKeyID(focused);
            foreach(object key in selected) {
                node = TreeList.FindNodeByKeyID(key);
                if(node != null)
                    TreeList.Selection.Add(node);
            }

        } finally {
            TreeList.EndUpdate();
            TreeListNode topVisibleNode = TreeList.FindNodeByKeyID(topNode);
            if(topVisibleNode == null) topVisibleNode = TreeList.FocusedNode;
            TreeList.TopVisibleNodeIndex = TreeList.GetVisibleIndexByNode(topVisibleNode);
        }
    }
    public void SaveState() {
        if(TreeList.FocusedNode != null) {
            expanded = GetExpanded();
            selected = GetSelected();
            focused = TreeList.FocusedNode[TreeList.KeyFieldName];
            topNode = TreeList.GetNodeByVisibleIndex(TreeList.TopVisibleNodeIndex)[TreeList.KeyFieldName];
        } else
            Clear();
    }

    private TreeList treeList;
    public TreeList TreeList {
        get {
            return treeList;
        }
        set {
            treeList = value;
            Clear();
        }
    }

    class OperationSaveExpanded : TreeListOperation {
        private ArrayList al = new ArrayList();
        public override void Execute(TreeListNode node) {
            if(node.HasChildren && node.Expanded)
                al.Add(node.GetValue(node.TreeList.KeyFieldName));
        }
        public ArrayList Nodes { get { return al; } }
    }
}

Save/Restore the State of Nodes

TreeListViewState treeListViewState;

private void Form1_Load(object sender, EventArgs e) {
    treeListViewState = new TreeListViewState(treeList1);
}

// Save the state of nodes.
private void saveNodeStateButton_Click(object sender, EventArgs e) {
    treeListViewState.SaveState();
}

// Restore the state of nodes.
private void loadNodeStateButton_Click(object sender, EventArgs e) {
    treeListViewState.LoadState();
}
See Also