Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > drag and drop the nodes of a tree view using ado.net in c#
 

drag and drop the nodes of a tree view using ado.net in c#

now my code implements the hierarchy of the employees that is as soon as i click the parent node i am able to get the child nodes.my table is Id ParentId Name
1 0 Satya
2 1 Deepak
3 0 RaviChandra
4 2 Ram
5 2 Ranjeeta
6 3 Swapna
now accordingly i am able to get a three level hierarchy

now, my problem is i need to drag and drop the nodes that is the employees .I am not able to appropriately code it using drag and drop events.

i ve been working on this issue for a lot of time plz help me out. if u have any doughts on my problem plz let me know.


Thanks in advance

swpa  Saturday, November 08, 2008 7:16 AM

Hi swps,

Since in your project, there is only one tree. So you need to change the sample code a little. Also don’t forget to set the AllowDrop property of your TreeView to true. Here is the code.

Code Snippet

partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

treeView1.AllowDrop = true;

treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);

treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);

treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);

}

void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

{

DoDragDrop(e.Item, DragDropEffects.Move);

}

void treeView1_DragEnter(object sender, DragEventArgs e)

{

e.Effect = DragDropEffects.Move;

}

void treeView1_DragDrop(object sender, DragEventArgs e)

{

TreeNode newNode;

if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))

{

Point pt;

TreeNode destinationNode;

pt = treeView1.PointToClient(new Point(e.X, e.Y));

destinationNode = treeView1.GetNodeAt(pt);

newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

if (!destinationNode.Equals(newNode))

{

//destinationNode.Nodes.Add(newNode.Clone());

destinationNode.Nodes.Add((TreeNode)newNode.Clone());

destinationNode.Expand();

//Remove original node

newNode.Remove();

}

}

}

You can download the whole solution from my SkyDrive TreeNodeDragDrop Sample, it works fine. Hope you can solve your problem from this post.

Sincerely,

Kira Qian

Kira Qian  Wednesday, November 12, 2008 7:15 AM

Hi swps

Welcome to MSDN again.

Here is an example. How do I implement Drag and Drop support between two TreeViews?

In your case, you have just one tree. So you can handle the DragEnter, ItemDrag and DragDrop event of the same TreeView.

Here is some FAQ, which will also show you some sample of TreeView. Please take it for reference. Windows Forms General FAQs

Please tell me if you have any question. Wish you can solve it soon.

Sincerely,

Kira Qian

Kira Qian  Monday, November 10, 2008 9:01 AM
Thanks for ur response Mr.Kira.Ive been applying the code but nothing is happening. Can u plz review the code uve sent .And

plz tell me if i have to make any changes.

thanks in advance


swpa  Wednesday, November 12, 2008 6:53 AM

Hi swps,

Since in your project, there is only one tree. So you need to change the sample code a little. Also don’t forget to set the AllowDrop property of your TreeView to true. Here is the code.

Code Snippet

partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

treeView1.AllowDrop = true;

treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);

treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);

treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);

}

void treeView1_ItemDrag(object sender, ItemDragEventArgs e)

{

DoDragDrop(e.Item, DragDropEffects.Move);

}

void treeView1_DragEnter(object sender, DragEventArgs e)

{

e.Effect = DragDropEffects.Move;

}

void treeView1_DragDrop(object sender, DragEventArgs e)

{

TreeNode newNode;

if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))

{

Point pt;

TreeNode destinationNode;

pt = treeView1.PointToClient(new Point(e.X, e.Y));

destinationNode = treeView1.GetNodeAt(pt);

newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

if (!destinationNode.Equals(newNode))

{

//destinationNode.Nodes.Add(newNode.Clone());

destinationNode.Nodes.Add((TreeNode)newNode.Clone());

destinationNode.Expand();

//Remove original node

newNode.Remove();

}

}

}

You can download the whole solution from my SkyDrive TreeNodeDragDrop Sample, it works fine. Hope you can solve your problem from this post.

Sincerely,

Kira Qian

Kira Qian  Wednesday, November 12, 2008 7:15 AM
Thank u Mr.KiraQian its excelently working the only change was the use of "this.treeView1.Allowdrop=true".by applying its

excelently working .



Thanku very much Mr.Kira
swpa  Wednesday, November 12, 2008 8:45 AM

You can use google to search for other answers

Custom Search

More Threads

• .net ODBC Provider
• DataBinding with the usercontrol made by combobox
• Auto general Row ?
• getting the cellValues of a datatbound DGV row before it is removed
• What is the difference between CellDoubleClick and CellMouseDoubleClick?
• How to use MSDBGrid or ADODC objects in VB.NET???
• DataGridView adds two new rows on 'escape'
• Data set bound to a control getChanges does not show the deltas
• Binding Problem with arraylist
• How to control Datagridview Vertical Scrollbar