Can any help me How to Drag and Drop node from treeview to Datagrid using c#
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.
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 ...
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.
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. -
-
this.dataGridView2.AllowDrop = true;
-
this.dataGridView1.DragEnter += new DragEventHandler(dataGridView1_DragEnter);
-
this.dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);
-
-
-
-
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
-
{
-
DoDragDrop(e.Item, DragDropEffects.Move);
-
}
-
-
-
-
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
-
{
-
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
-
{
-
e.Effect = DragDropEffects.Copy;
-
}
-
}
-
-
-
-
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
-
{
-
DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow))
-
as DataGridViewRow;
-
if (row != null)
-
{
-
DataGridViewRow newrow = row.Clone() as DataGridViewRow;
-
for (int i = 0; i < newrow.Cells.Count; i++)
-
{
-
newrow.Cells[i].Value = row.Cells[i].Value;
-
}
-
this.dataGridView1.Rows.Add(newrow);
-
-
}
-
}
-
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. - private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
-
{
-
//DoDragDrop(treeView1.SelectedNode.Name, DragDropEffects.Copy); // i believe this is where the problem comes from
-
-
dragging = true;
-
row = new object();
-
treeView1.SelectedNode = (TreeNode) e.Item;//dragging doesn't automatically change the selected index
-
row = treeView1.SelectedNode.Name;//or whatever value you need from the node
-
-
//here you could set the cursor to something else to reflect that you are dragging
-
}
-
-
-
-
private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
-
{
-
-
if (dragging)
-
{
-
dataGridView1.Rows.Add(row);//you could some formatting here to get the right details into the right column
-
dragging = false;
-
-
//set your cursor back to the deafault
-
}
-
}
I hope this helps you.
the datagrid doesn't need to accept drop now and you need only two event handlers
good luck
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
cloud255 427
Recognized Expert Contributor
Hey,
happy to help, here you go: - public partial class Form1 : Form
-
{
-
//these declerations need to happen inside of the class but outside of any methods in the class
-
-
private object _row;
-
-
public object row //this is a property decleration
-
{
-
private get{ return this._row; }
-
private set{ this._row = value; }
-
}
-
-
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
Hey,
happy to help, here you go: - public partial class Form1 : Form
-
{
-
//these declerations need to happen inside of the class but outside of any methods in the class
-
-
private object _row;
-
-
public object row //this is a property decleration
-
{
-
private get{ return this._row; }
-
private set{ this._row = value; }
-
}
-
-
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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,
|
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)
| |
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...
|
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.
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |