Drag and Drop of treenode in a treeview

First Call the ItemDrag event of the source tree view and call the DoDrag method within that event. Then call the DragEnter event of the destination tree view. Finally, add the following code to the DragDrop event of the destination tree view.

//------Add this code in the DragDrop Event------

TreeNode trNode = null;

if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
Point objPoint = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode trNodeDestination = ((TreeView)sender).GetNodeAt(objPoint);
//If no node from the destination tree view is selected then add the selected node to the
//last node of the destination tree view.
if (trNodeDestination == null)
trNodeDestination = ((TreeView)sender).Nodes[((TreeView)sender).Nodes.Count - 1].LastNode;
trNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if (trNodeDestination.TreeView != trNode.TreeView)
//Remove Original Node


Author: Kapil Dhawan18 Jun 2008 Member Level: Gold   Points : 2

Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: