Skip to content

DevExpress-Examples/winforms-treelist-sync-position-of-nodes-with-data-records

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms TreeList - Sync the position of child nodes with the position of corresponding records in the data source

The TreeList control allows users to reorder nodes by dragging them with the mouse. If the user moves a node from one parent to another, the node's position is automatically saved in the data source (because it depends on the ParentID column value that is stored in the data source). When the user reorders the nodes in a child collection, the position of the nodes is reset after the application is closed and reopened (or after the data is reloaded).

To store the position of child nodes, do the following:

  • Add an additional column to the data source to hold node indexes (in this example this is the Order column).

  • Handle the TreeList's AfterDragNode event to save the new position of a node after the user has moved it to another position.

     private void treeList1_AfterDragNode(object sender, AfterDragNodeEventArgs e) {
         SaveNewRecordPosition(e);
     }
     private void SaveNewRecordPosition(NodeEventArgs e) {
         var nodes = e.Node.ParentNode == null ? e.Node.TreeList.Nodes
                     : e.Node.ParentNode.Nodes;
         for(var i = 0; i < nodes.Count; i++) {
             nodes[i].SetValue(colSort, i);
         }
     }
  • Iterate through child nodes and update their position using the TreeList.SetNodeIndex method when the application is loaded.

    private void UpdateNodesPositions(TreeListNodes nodes) {
        var ns = new List<TreeListNode>();
        foreach (TreeListNode n in nodes) {
            ns.Add(n);
        }
        foreach (TreeListNode n in ns) {
            UpdateNodesPositions(n.Nodes);
            n.TreeList.SetNodeIndex(n, Convert.ToInt32(n.GetValue("Order")));
        }
    }

Note

Newer versions have the following changes:

  • The signature of the AfterDragNode event's delegate has been changed.
  • TreeList's OptionsBehavior.DragNodes property is now obsolete. Use the OptionsDragAndDrop.DragNodesMode property instead.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)