473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to Drag and Drop node from treeview to Datagrid

Can any help me How to Drag and Drop node from treeview to Datagrid using c#
Jul 9 '08 #1
10 8846
cloud255
427 Expert 256MB
So i think what you want to do is represent the data shown in the node in a datagrd.

The first step would be to determine which node was clicked, then using an id, the entire object represented by the node or just the node text (a string) add that value to the datagrid.

perhaps if you had some code with a specific problem we could provide a more precise solution.
Jul 9 '08 #2
So i think what you want to do is represent the data shown in the node in a datagrd.

The first step would be to determine which node was clicked, then using an id, the entire object represented by the node or just the node text (a string) add that value to the datagrid.

perhaps if you had some code with a specific problem we could provide a more precise solution.

Hi Thanks for the reply.If you could help me a with a sample C# code to Drag and Drop node from treeview to Datagrid It'll be a great help...

Thanks in advance ...
Jul 10 '08 #3
cloud255
427 Expert 256MB
Hi Thanks for the reply.If you could help me a with a sample C# code to Drag and Drop node from treeview to Datagrid It'll be a great help...

Thanks in advance ...
I am more than willing to try and find an error in YOUR code and to help you fix it, try something first.
Jul 10 '08 #4
I am more than willing to try and find an error in YOUR code and to help you fix it, try something first.

Hi,

I tried with the follwong code :

1.created treeview control named treeView1
2.created event handler ItemDrag

3.Created datagrid named dataGridView1
4.Made it allow drop = true
5.created event handlers DragEnter & DragDrop

Eventhough I tried with a piece of code for the event DragDrop it's not working .It'll be a great help if you can guide me how to proceed with DragDrop in the gridview.

Expand|Select|Wrap|Line Numbers
  1.  
  2. this.dataGridView2.AllowDrop = true;
  3. this.dataGridView1.DragEnter += new DragEventHandler(dataGridView1_DragEnter);
  4. this.dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);
  5.  
  6.  
  7.  
  8. private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
  9. {
  10. DoDragDrop(e.Item, DragDropEffects.Move);
  11. }
  12.  
  13.  
  14.  
  15. private void dataGridView1_DragEnter(object sender, DragEventArgs e)
  16. {
  17. if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
  18. {
  19. e.Effect = DragDropEffects.Copy;
  20. }
  21. }
  22.  
  23.  
  24.  
  25. private void dataGridView1_DragDrop(object sender, DragEventArgs e)
  26. {
  27. DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow))
  28. as DataGridViewRow;
  29. if (row != null)
  30. {
  31. DataGridViewRow newrow = row.Clone() as DataGridViewRow;
  32. for (int i = 0; i < newrow.Cells.Count; i++)
  33. {
  34. newrow.Cells[i].Value = row.Cells[i].Value;
  35. }
  36. this.dataGridView1.Rows.Add(newrow);
  37.  
  38. }
  39. }
  40.  
Jul 10 '08 #5
cloud255
427 Expert 256MB
Hey there,

this is real nice problem,

I tried your code, it seems to me that windows doesn't want you dragging a node to a datagrid. I think this problem comes from the DragDropEffects value.

I have a work around that i think will be able to do what you want, the only problem is that the mouse cursor does not change to reflect that you are dragging, you will have to manually do some cursor setting. Anyway here does:

Create a public boolean (dragging) and a property of type object (row) with a private get and a private set.

intialize dragging to false in the decleration.

Expand|Select|Wrap|Line Numbers
  1. private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
  2.         {
  3.             //DoDragDrop(treeView1.SelectedNode.Name, DragDropEffects.Copy); // i believe this is where the problem comes from
  4.  
  5.            dragging = true;
  6.             row = new object();
  7.             treeView1.SelectedNode = (TreeNode) e.Item;//dragging doesn't automatically change the selected index
  8.             row = treeView1.SelectedNode.Name;//or whatever value you need from the node
  9.  
  10.            //here you could set the cursor to something else to reflect that you are dragging
  11.         }
  12.  
  13.  
  14.  
  15. private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
  16.         {
  17.  
  18.  if (dragging)
  19.             {
  20.                  dataGridView1.Rows.Add(row);//you could some formatting here to get the right details into the right column
  21.                 dragging = false;
  22.  
  23.                 //set your cursor back to the deafault
  24.             }
  25. }
I hope this helps you.

the datagrid doesn't need to accept drop now and you need only two event handlers

good luck
Jul 10 '08 #6
Hi Cloud ,
Thanks for the valuable reply .... I didn't understand the part "Create a public boolean (dragging) and a property of type object (row) with a private get and a private set.intialize dragging to false in the decleration."
Can u please send that piece of code ....

Thanks in Advance
Jul 11 '08 #7
cloud255
427 Expert 256MB
Hey,

happy to help, here you go:

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.        //these declerations need to happen inside of the class but outside of any methods in the class
  4.  
  5.        private object _row;
  6.  
  7.        public object row //this is a property decleration
  8.        {
  9.            private get{ return this._row; }
  10.            private set{ this._row = value; }
  11.        }
  12.  
  13.        public bool dragging = false; //this is your global boolean
note i did not include a closing bracket for the class as all the other code goes under this.

good luck
Jul 11 '08 #8
Hey,

happy to help, here you go:

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.        //these declerations need to happen inside of the class but outside of any methods in the class
  4.  
  5.        private object _row;
  6.  
  7.        public object row //this is a property decleration
  8.        {
  9.            private get{ return this._row; }
  10.            private set{ this._row = value; }
  11.        }
  12.  
  13.        public bool dragging = false; //this is your global boolean
note i did not include a closing bracket for the class as all the other code goes under this.

good luck

Hi Cloud ,
Thanks a bunch for your support ...
I tried the code but got excetion like - “Cannot specify accessibility modifiers for both accessors of the property or indexer”
I removed the "private" from private set{ this._row = value; } and was able to successfully build the soln.
BUT HARD LUCK.it's stll not working ...The drag nd drop functionality to the grid view is not at all happening ...If any soultions you are able find please post...

Once again Thanks a million
Jul 11 '08 #9
Hey dude,
I got it working !!!! Actually i re-written all the event handlers ... its working perfectly (idunno whts the real peoblem ... same code written once agains ...starts working ...) ...thaks dude for your support ....

Thanks
Jul 11 '08 #10
cloud255
427 Expert 256MB
glad to be of assistence.

Good luck with the project.
Jul 11 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: EggHead | last post by:
Hi all, I am trying to implement Drag and Drop with my TreeView Control. The problem is, when I click on a node, drag it, and drop it to other node. The dropped node does not hold the drop...
0
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly...
1
by: Kevin Henkener | last post by:
I would like to be able to drag-n-drop System.Windows.Forms.TreeNode objects between two different applications (actually the same application, just two instances of it) in C#. I've seen posts in...
0
by: Norbert Heidbüchel | last post by:
Hi all, I have read a lot of postings and web pages about drag and drop and treeviews, but could not find an answer to my problem. Sorry, if I missed something. I am trying to drag and drop...
1
by: emferrari | last post by:
Hi everybody I have two treeviews, one of them is only to receive items dragged from the treeview1. I want to know how to drag a full node information to the treeview2. I know how to do that...
1
by: Terry Olsen | last post by:
My first time using TreeViews. I have TreeView1 set up to display my directory structure just like Windows Explorer. I can drag & drop files and folders over to TreeView2. I can re-arrange the...
1
by: pooja | last post by:
i need to implement drag and drop in treeview in VB. Kindly help. My treeview contains activities maintained using XML Files. Hopefully, Thanks.
1
by: timnels | last post by:
I have created a muti-select treeview control. Problem is I am now trying to implement drag/drop in the application that uses it. It seems the mouse down and mouse move events fire before the...
2
by: bob | last post by:
Hi all, I have a treeview that has drag drop. Works well enough but... If you drag out of bounds of the treeview the nodrop icon comes on. Fair enough. But when I move back inside the treeview...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.