KB Article A342
Visible to All Users

How to reorder XtraTreeList nodes via drag-and-drop

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 Basic
Imports 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

Show previous comments (1)
DevExpress Support Team 11 years ago

    Hello,
    I see that you already created a separate ticket Q573961 related to this issue. Let's continue our discussion there.

    BS BS
    Behzad Samadianpour 2 8 years ago

      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)?

      DevExpress Support Team 8 years ago

        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.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.