Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to change a TreeList node position along with a corresponding record's position in the database

$
0
0

TreeList allows users to reorder nodes by dragging them with the mouse. If a node was moved from one parent to another, its position will be saved automatically, because it depends on the ParentID column value which is stored in the database. However, when nodes are reordered within the child collection, their position will be reset after the application is closed and opened again, or after reloading data.

To keep nodes positions, it is necessary to add additional column to the datasource to store the node index. In this situation, nodes positions can be restored after loading the data into the TreeList. This task can be accomplished by iterating all the nodes and updating the node index via the TreeList.SetNodeIndex method.

[C#]
voidUpdateNodesPositions(TreeListNodesnodes){List<TreeListNode>ns=newList<TreeListNode>();foreach(TreeListNodeninnodes)ns.Add(n);foreach(TreeListNodeninns){UpdateNodesPositions(n.Nodes);n.TreeList.SetNodeIndex(n,Convert.ToInt32(n.GetValue("Order")));}}
[VB.NET]
PrivateSub UpdateNodesPositions(ByVal nodes As TreeListNodes)Dim ns AsNew List(Of TreeListNode)()ForEach n As TreeListNode In nodes ns.Add(n)Next nForEach n As TreeListNode In ns UpdateNodesPositions(n.Nodes) n.TreeList.SetNodeIndex(n, Convert.ToInt32(n.GetValue("Order")))Next nEndSub

 
Note that the UpdateNodesPositions method should be called after TreeList is populated with data.  It's also necessary to handle the AfterDragNode event and update the "Order" field's values as follows:

[C#]
privatevoidtreeList1_AfterDragNode(objectsender,AfterDragNodeEventArgse){SaveNewRecordPosition(e);}privatevoidSaveNewRecordPosition(NodeEventArgse){varnodes=e.Node.ParentNode==null?e.Node.TreeList.Nodes:e.Node.ParentNode.Nodes;for(vari= 0;i<nodes.Count;i++){nodes[i].SetValue(colSort,i);}}
[VB.NET]
PrivateSub treeList_AfterDragNode(ByVal sender AsObject, ByVal e As AfterDragNodeEventArgs) Handles treeList1.AfterDragNode SaveNewRecordPosition(e) EndSubPrivateSub SaveNewRecordPosition(ByVal e As NodeEventArgs) Dim nodes = If(e.Node.ParentNode IsNothing, e.Node.TreeList.Nodes, e.Node.ParentNode.Nodes) For i = 0 To nodes.Count - 1 nodes(i).SetValue(colSort, i) Next i EndSub

Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>