Description:
Is it possible to use drag&drop to reorder rows?
Answer:
[Updated]
Starting with version 15.2, this functionality is accessible out of the box. To enable it, set the TreeList.OptionsDragAndDrop.DragNodesModeproperty to Single or Multiple.
[For versions older than 15.2]
The attached sample projects demonstrate how to override the default drag&drop handling in an unbound XtraTreeList and allow users to reorder nodes only within a given parent node.
The TreeList has the automatic drag&drop support. You can utilize it without writing additional code. However, some tasks may require you to have control over drag&drop operations. Fortunately, the TreeList provides you with the necessary API for accomplishing such tasks.
In this case, you should use the DragOver event and change the e.Effect parameter, if you need to conditionally enable/disable the drop operation. Set e.Effect to DragDropEffects.None to disable the drop operation. The DragDrop event will not be fired in this case.
The DragDrop event is used to handle dropping an object at a given position. Our sample project utilizes it to move a node to a new location via the SetNodeIndex method of the TreeList class. Finally, the e.Effect parameter should be set to DragDropEffects.None to notify the TreeList of that the drag&drop operation is handled and the default processing is not needed.
To calculate a value for e.Effect, you may need to know which node resides under the mouse cursor. Use the CalcHitInfo method of TreeList for this. Pay attention that screen coordinates are passed to the DragOver and DragDrop events. Before you call CalcHitInfo, you must convert screen coordinates to the TreeList coordinates via the the PointToClient method.
C#using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
private void treeList1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
TreeListNode dragNode, targetNode;
TreeList tl = sender as TreeList;
Point p = tl.PointToClient(new Point(e.X, e.Y));
dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
targetNode = tl.CalcHitInfo(p).Node;
tl.SetNodeIndex(dragNode, tl.GetNodeIndex(targetNode));
e.Effect = DragDropEffects.None;
}
Visual BasicImports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes
Private Sub TreeList1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeList1.DragDrop
Dim dragNode, targetNode As TreeListNode
Dim tl As TreeList = CType(sender, TreeList)
Dim p As Point = tl.PointToClient(New Point(e.X, e.Y))
dragNode = e.Data.GetData(GetType(TreeListNode))
targetNode = tl.CalcHitInfo(p).Node
tl.SetNodeIndex(dragNode, tl.GetNodeIndex(targetNode))
e.Effect = DragDropEffects.None
End Sub
See Also:
How to insert a TreeList node into a specific position
How to change XtraTreeList drag-and-drop icons
How to reorder XtraTreeList nodes via drag-and-drop
How to implement drag and drop between two XtraTreeList controls
TreeList.CustomizeNewNodeFromOuterData Event
Hi, This is an excellent article for a learner like me.
I need help on how to move a child node from one parent to the other.
i am very thankful if got any workaround
thanks
Hello,
I see that you already created a separate ticket Q573961 related to this issue. Let's continue our discussion there.
I have the same question regarding dragging and dropping child node from one parent to another. I cannot view the ticket Q573961 because it looks like the ticket has been classified as private.
Can you please update, if such behaviour in XtraTreeList is supported (i.e. moving child node from one parent to another by drag drop)?
Hello Behzad,
To avoid any misunderstanding, let's continue discussion of this question in the How to implement the TreeList - How to drag and drop child nodes from one parent to another ticket.