473,799 Members | 3,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Drag and Drop node from treeview to Datagrid

6 New Member
Can any help me How to Drag and Drop node from treeview to Datagrid using c#
Jul 9 '08 #1
10 8923
cloud255
427 Recognized Expert Contributor
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
thomsonsv
6 New Member
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 Recognized Expert Contributor
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
thomsonsv
6 New Member
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 Recognized Expert Contributor
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
thomsonsv
6 New Member
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 Recognized Expert Contributor
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
thomsonsv
6 New Member
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
thomsonsv
6 New Member
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

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

Similar topics

1
18535
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 postion. It moves to the top of the nodes. BTW, I am using VB6.0 here. here is the codes ------------------ Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As...
0
2279
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 delicately balanced and finding my way through it has descended into one of those awful situations which I refer to as guess & test. From the point at which DoDragDrop() function is called until some later
1
2542
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 which people advise that this process is no different the intra-application drag-n-drop process. I've had no luck with this concept. In fact, when I drag a node from application A's TreeView to application B's TreeView and call the GetData...
0
2421
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 treenodes defined by myself and don't understand, how to get the nodes data in the dragdrop event. I'm not very experienced in .NET and for sure there is a simple solution. I've written a short sample program, based on well known examples,
1
3506
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 only in a single node operation. Here is my current code: private void trv_Directories_ItemDrag(object sender, ItemDragEventArgs e)
1
2315
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 structure in TreeView2 (putting files in different folders and such...like you do when you create a structure for burning a CD). I have two questions. 1. How can I remember the full path from TreeView1 (when it comes time to actually do...
1
1881
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
5017
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 OnBeforeSelect and OnAfterSelect events in the treeview. Since I want to start the drag/drop on the mouse move event, I have no clue that the current node has been selected yet. I tried moving all the code into the mouse down event (which actually...
2
3376
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 the nodrop icon stays on and essentially the action is cancelled when I release the mouse. I would like to somehow 'retrieve' the drag action when the mouse moves back inside the treeview to a legitimate drop point. Any thoughts on how to do this...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.