Skip to content

Instantly share code, notes, and snippets.

@turtlepile
Created June 9, 2022 20:05
Show Gist options
  • Save turtlepile/6eda8faaa3ee615c71fe327586637e84 to your computer and use it in GitHub Desktop.
Save turtlepile/6eda8faaa3ee615c71fe327586637e84 to your computer and use it in GitHub Desktop.
Example for saving & loading node states with DevExpress TreeList.
using System;
namespace Example
{
public partial class dxMyForm : DevExpress.XtraBars.Ribbon.RibbonForm
{
private TreeListViewState treeListViewState;
public dxMyForm()
{
InitializeComponent();
}
private void dxMyForm_Load(object sender, EventArgs e)
{
treeListViewState = new TreeListViewState(treeList1);
LoadData();
}
private void LoadData()
{
treeList1.DataSource = DataTable..
}
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
{
treeListViewState.SaveState();
}
private void treeList1_NodesReloaded(object sender, EventArgs e)
{
treeListViewState.LoadState();
}
}
}
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; } }
}
}
@turtlepile
Copy link
Author

Add [TreeListViewState.cs] to your project than use it as in example..
Tricky part is you need to load the nodes treeList1_NodesReloaded event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment