---
title: "Drag-Drop in TreeView in C#"  
description: "This article will briefly describe the basics of drag and drop in TreeView Control or Draggable TreeView Nodes in C#, the aim of the article is to des"  
author: "AVADHESH PATEL"  
published: 2012-07-20  
updated: 2020-07-11  
canonical: https://www.mindstick.com/articles/963/drag-drop-in-treeview-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Drag-Drop in TreeView in C#

This article will briefly describe the basics of drag and drop in [TreeView](https://www.mindstick.com/forum/1180/how-to-create-a-treeview-from-xml-file-in-c-sharp) Control or Draggable TreeView Nodes in C#, the aim of the article is to describe how node dragging and swapping in TreeView, implemented in C#.\

Steps: - Take a windows form and drag drop a TreeView Control and write code which is given below

![Drag-Drop in TreeView in C#](https://www.mindstick.com/mindstickarticle/ac6a8a90-4435-4d03-84b5-0c0cebe402b4/images/7bd9a0a7-702f-4a2c-95f0-63272c2d8cd3.png)

##### Code:-

```
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace TreeViewDragDrop{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            TreeNode rootNode = treeView1.Nodes.Add("USA");             // Create the Child nodes for the root            TreeNode states1 = rootNode.Nodes.Add("New York");            TreeNode states2 = rootNode.Nodes.Add("Michigan");            TreeNode states3 = rootNode.Nodes.Add("Wisconsin");            TreeNode states4 = rootNode.Nodes.Add("California");             // Create siblings nodes for the Child nodes            TreeNode child = states1.Nodes.Add("Rochester");            child = states1.Nodes.Add("New York");            child = states1.Nodes.Add("Albany");            child = states2.Nodes.Add("Detroit");            child = states2.Nodes.Add("Ann Arbor");            child = states2.Nodes.Add("Lansing");            child = states3.Nodes.Add("Milwaukee");            child = states3.Nodes.Add("Madison");            child = states3.Nodes.Add("La Cross");            child = states4.Nodes.Add("Los Angeles");            child = states4.Nodes.Add("San Fransisco");            child = states4.Nodes.Add("San Diego");             // Expand all tree nodes when first loaded            treeView1.AllowDrop = true;            treeView1.ExpandAll();          }         private void Form1_Load(object sender, EventArgs e)        {        }         private void treeView1_DragEnter(object sender, DragEventArgs e)        {            e.Effect = DragDropEffects.Move;        }         private void treeView1_DragDrop(object sender, DragEventArgs e)        {            TreeNode SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");            Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));            TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);                       try            {                int sourceIndex = SourceNode.Index;                int destIndex = DestinationNode.Index;                 if (SourceNode.Level == 1 && DestinationNode.Level == 1)                {                    if (sourceIndex < destIndex)                    {                        TreeNode parentNode = SourceNode.Parent;                        SourceNode.Remove();                        DestinationNode.Remove();                        parentNode.Nodes.Insert(sourceIndex, DestinationNode);                        parentNode.Nodes.Insert(destIndex, SourceNode);                    }                    else                    {                        TreeNode parentNode = SourceNode.Parent;                        SourceNode.Remove();                        DestinationNode.Remove();                        parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);                        parentNode.Nodes.Insert(destIndex, SourceNode);                    }                }                 else if (SourceNode.Level == 2 && DestinationNode.Level == 2)                {                                       if (SourceNode.Parent == DestinationNode.Parent)                    {                        if (sourceIndex < destIndex)                        {                            TreeNode parentNode = SourceNode.Parent;                            SourceNode.Remove();                            DestinationNode.Remove();                            parentNode.Nodes.Insert(sourceIndex, DestinationNode);                            parentNode.Nodes.Insert(destIndex, SourceNode);                        }                        else                        {                            TreeNode parentNode = SourceNode.Parent;                            SourceNode.Remove();                            DestinationNode.Remove();                            parentNode.Nodes.Insert(sourceIndex - 1, DestinationNode);                            parentNode.Nodes.Insert(destIndex, SourceNode);                        }                    }                    else                    {                        TreeNode addNode = DestinationNode.Parent;                        SourceNode.Remove();                        addNode.Nodes.Insert(destIndex, SourceNode);                                           }                }                 else if (SourceNode.Level == 2 && DestinationNode.Level == 1)                {                     if (DestinationNode.Nodes.Count==0)                    {                        SourceNode.Remove();                        DestinationNode.Nodes.Insert(0, SourceNode);                                   }                    else                    {                        int i = DestinationNode.LastNode.Index;                        SourceNode.Remove();                        DestinationNode.Nodes.Insert(i + 1, SourceNode);                    }                }            }            catch            {                MessageBox.Show("Drag on node...");            }        }         private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)        {            DoDragDrop(e.Item, DragDropEffects.Move);        }          private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)        {         }    }}
```

Output:-

\

![Drag-Drop in TreeView in C#](https://www.mindstick.com/mindstickarticle/ac6a8a90-4435-4d03-84b5-0c0cebe402b4/images/105cab2f-0b45-45ec-8108-02bf4e08fe85.png)

\

---

Original Source: https://www.mindstick.com/articles/963/drag-drop-in-treeview-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
