This is a migrated thread and some comments may be shown as answers.

Need winform example: Drag from Grid to Treeview

17 Answers 485 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Richard Thurgood
Top achievements
Rank 1
Richard Thurgood asked on 18 Jul 2008, 06:52 PM
I'd sure appreciate some help. I see some examples for web applications but I need a simple way to drag/drop a selected row from a grid into a treeview on a winform.

I don't see any properties on the grid that allow drag and I have the lastest evaluation version installed.

Anyone? 

17 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 22 Jul 2008, 08:22 AM
Hi Richard Thurgood,

We are working on a drag and drop framework for RadControls for WinForms which will allow easy implementation of drag and drop functionality. Unfortunately I cannot give you a specific time frame at the moment, but I think we could estimate that we'll have it for Q3 2008.

In the meantime you can use the drag and drop functionality that is available in Windows Forms to implement dragging from a RadGridView to a RadTreeView control like that:
protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e); 
 
            this.radGridView1.Rows.Add("Name1""Value1"); 
            this.radGridView1.Rows.Add("Name2""Value2"); 
            this.radGridView1.Rows.Add("Name3""Value3"); 
 
            this.radTreeView1.AllowDrop = true
            this.radGridView1.MouseDown += new MouseEventHandler(radGridView1_MouseDown); 
            this.radTreeView1.DragEnter += new DragEventHandler(radTreeView1_DragEnter); 
            this.radTreeView1.DragDrop += new DragEventHandler(radTreeView1_DragDrop); 
        } 
 
        void radGridView1_MouseDown(object sender, MouseEventArgs e) 
        { 
            GridViewRowInfo currentRow = this.radGridView1.MasterGridViewInfo.CurrentRow; 
            radGridView1.DoDragDrop(currentRow.Cells[0].Value, DragDropEffects.Copy); 
        } 
 
        void radTreeView1_DragDrop(object sender, DragEventArgs e) 
        { 
            string text = e.Data.GetData(DataFormats.Text).ToString(); 
            RadTreeNode newNode = new RadTreeNode(text); 
 
            Point p = this.radTreeView1.PointToClient(new Point(e.X, e.Y)); 
            RadTreeNode targetNode = this.radTreeView1.GetNodeAt(p.X, p.Y); 
 
            RadTreeNodeCollection targetCollection = (targetNode != null) ? targetNode.Nodes : this.radTreeView1.Nodes; 
            targetCollection.Add(newNode); 
        } 
 
        void radTreeView1_DragEnter(object sender, DragEventArgs e) 
        { 
            e.Effect = (e.Data.GetDataPresent(DataFormats.Text)) ? DragDropEffects.Copy : DragDropEffects.None; 
        } 

Hope this helps.

All the best,
Jordan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Richard Thurgood
Top achievements
Rank 1
answered on 22 Jul 2008, 12:33 PM
That's fantastic!  THANKS!

(NOTE: I had to add columns to the code prior to adding the data, but the sample code was just what I needed)
0
Richard Thurgood
Top achievements
Rank 1
answered on 31 Jul 2008, 12:07 AM
Uh oh - I thought this was going to work but now I see that I have issues with selecting rows, sorting and scrolling since the event is hooked to "MouseDown".  Any other suggestions? 
0
Jordan
Telerik team
answered on 31 Jul 2008, 10:11 AM
Hi Richard,

I tested the project again and indeed found a bug that present when you try to scroll or sort.
The solution is to merely check if the current row is not null :
void radGridView1_MouseDown(object sender, MouseEventArgs e) 
        { 
            GridViewRowInfo currentRow = this.radGridView1.MasterGridViewInfo.CurrentRow; 
            if (currentRow != null
            { 
                radGridView1.DoDragDrop(currentRow.Cells[0].Value, DragDropEffects.Copy); 
            } 
        } 

If that is not what you need, please write back with some more details about the problems that you are experiencing.

Kind regards,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Richard Thurgood
Top achievements
Rank 1
answered on 31 Jul 2008, 02:31 PM
Actually, I already did that. I found that issue shortly after I tried the sample code so I rectified it prior to my post.  That said, you still can't sort, select or scroll properly once you've tied into the mouse down event. 

For example, select a row and then try to sort or scroll. You'll see it doesn't function properly.

Suggestions?
0
Accepted
Jordan
Telerik team
answered on 01 Aug 2008, 08:29 AM
Hello Richard Thurgood,

It turned out that in order to have the problems solved, you must call  DoDragDrop in the mouse move handler. Here is the solution that I suggest:
private Point startPoint = Point.Empty; 
        private GridViewRowInfo currentRow = null
 
        void radGridView1_MouseDown(object sender, MouseEventArgs e) 
        { 
            if (this.radGridView1.GridElement.VScrollBar.ControlBoundingRectangle.Contains(e.Location)) 
            { 
                return
            } 
            if (e.Button != MouseButtons.Left) 
            { 
                return
            } 
            this.currentRow = this.radGridView1.MasterGridViewInfo.CurrentRow; 
            if (this.currentRow == null
            { 
                return
            } 
 
            this.startPoint = e.Location; 
        } 
 
        void radGridView1_MouseMove(object sender, MouseEventArgs e) 
        { 
            if (e.Button != MouseButtons.Left) 
            { 
                return
            } 
 
            if (this.radGridView1.GridElement.VScrollBar.ControlBoundingRectangle.Contains(e.Location)) 
            { 
                return
            } 
 
            if (Math.Abs(e.X - this.startPoint.X) < SystemInformation.DragSize.Width 
              && Math.Abs(e.Y - this.startPoint.Y) < SystemInformation.DragSize.Height) 
            { 
                return
            } 
 
            if (this.currentRow == null
            { 
                return
            } 
 
            radGridView1.DoDragDrop(this.currentRow.Cells[0].Value, DragDropEffects.Copy); 
        } 

Please give it a try and write me back if you need more assistance.

Sincerely yours,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Richard Thurgood
Top achievements
Rank 1
answered on 08 Aug 2008, 02:09 PM
Thanks for your reply. Yes, this worked. I added:

if (this.radGridView1.GridElement.HScrollBar.ControlBoundingRectangle.Contains(e.Location))
{
 return;
}


...to handle the horizontal scrolling like you did with the vertial scrolling. Well, it works for now and I'll update the code when you have the DragDrop released in Q3.

Thanks again!

0
Jordan
Telerik team
answered on 08 Aug 2008, 02:36 PM
Hi Richard,

Glad to see that I could help.
Just to clarify, we are still evaluating the Drag and Drop framework and I cannot promise you that it will be available in the Q3 2008 release.

Kind regards,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ilya
Top achievements
Rank 1
answered on 02 Oct 2008, 10:02 AM
Hi there...

I've got all this working from a GridView to nodes in a tree.
On mouse over each of the nodes while dragging from the grid, i really need for each node to highlight. At the moment there is not good visual feedback to show that the item from the grid will be dropping into a specific node.

Can you shed some light on this?

Thanks,
ilya.
0
Jordan
Telerik team
answered on 03 Oct 2008, 12:00 PM
Hi Ilya,

The drop indication that you may have seen in the examples is a part of the RadTreeView drag and drop functionality. Unfortunately, it is not available for usage outside the treeview.
 
A solution I can suggest is to try changing the the cursor when the mouse is over a node in order to give visual feedback.

 
Sincerely yours,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jure
Top achievements
Rank 1
answered on 03 Feb 2009, 02:58 PM
Hi.

Excellent code, thanks.

Here's the code to highlight a node that is being dragged over (VB.net - use converter for C#):
'class level 
Private currentNode As RadTreeNode 
 
Private Sub tree_DragOver(ByVal sender As ObjectByVal e As System.Windows.Forms.DragEventArgs) Handles tree.DragOver 
        Dim p As Point = Me.tree.PointToClient(New Point(e.X, e.Y)) 
 
        If Not currentNode Is Nothing Then 
            currentNode.BackColor = Color.Transparent 
        End If 
 
        currentNode = Me.tree.GetNodeAt(p.X, p.Y) 
        currentNode.BackColor = Color.OrangeRed 
 
    End Sub 

Edit: Here's the relevant (though not too clean) code for efficient (only data rows and not header or scrollbar) dragging:

 Private Sub gridView1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles gridView1.MouseDown 
        If Me.gridView1.GridElement.VScrollBar.ControlBoundingRectangle.Contains(e.Location) Then 
            Return 
        End If 
 
        If (Not e.Button = Windows.Forms.MouseButtons.Left) Then 
            Return 
        End If 
 
        'must be GridDataCellElement and NOT GridHeaderCellElement to drag & drop 
        If TypeOf Me.gridView1.ElementTree.GetElementAtPoint(e.Location) Is GridDataCellElement And _ 
           (Not TypeOf Me.gridView1.ElementTree.GetElementAtPoint(e.Location) Is GridHeaderCellElement) Then 
            Me.currentRow = Me.gridView1.MasterGridViewInfo.CurrentRow 
        Else 
            Me.currentRow = Nothing 
        End If 
 
        If Me.currentRow Is Nothing Then 
            Return 
        End If 
        Me.startPoint = e.Location 
 
    End Sub 
 
    Private Sub gridView1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles gridView1.MouseMove 
        If Not e.Button = Windows.Forms.MouseButtons.Left Then 
            Return 
        End If 
        If Me.gridView1.GridElement.VScrollBar.ControlBoundingRectangle.Contains(e.Location) Then 
            Return 
        End If 
        If (Math.Abs(e.X - Me.startPoint.X) < SystemInformation.DragSize.Width) And (Math.Abs(e.Y - Me.startPoint.Y) < SystemInformation.DragSize.Height) Then 
            Return 
        End If 
        If Me.currentRow Is Nothing Then 
            Return 
        End If 
 
        'must be GridDataCellElement to drag & drop 
        If TypeOf Me.gridView1.ElementTree.GetElementAtPoint(e.Location) Is GridDataCellElement Then 
            Me.gridView1.DoDragDrop(Me.currentRow.Cells(0).Value, DragDropEffects.Copy) 
        End If 
 
    End Sub 



0
Jay
Top achievements
Rank 1
answered on 05 Aug 2009, 03:17 PM
Has the framework been extended thus far to allow this to work without the windows events?

I'm trying to drag from a treeview to the gridview and none of the gridview events are firing...

please let me know

thanks!
0
Victor
Telerik team
answered on 10 Aug 2009, 11:20 AM
Hello Jure,

Please elaborate on what do you mean by none of the gridview events are firing. I tested drag and drop between RadTreeView and RadGridView and it works correctly. Here is the code that I am testing, feel free to use it as needed:

private void radGridView1_DragEnter(object sender, DragEventArgs e)  
{  
    if (e.Data.GetDataPresent(typeof(string)))  
    {  
        e.Effect = DragDropEffects.Copy;  
    }  
    else 
    {  
        e.Effect = DragDropEffects.None;  
    }  
}  
 
private void radGridView1_DragDrop(object sender, DragEventArgs e)  
{  
    GridViewRowInfo newRow = this.radGridView1.Rows.AddNew();  
    newRow.Cells[0].Value = e.Data.GetData(typeof(string));  
}  
 
private void radTreeView1_MouseDown(object sender, MouseEventArgs e)  
{  
    RadTreeNode clickedNode = this.radTreeView1.GetNodeAt(e.X, e.Y);  
    if (clickedNode != null)  
    {  
        this.DoDragDrop(clickedNode.Text, DragDropEffects.Copy);  
    }  

Basically the logic above copies the text from the dragged node and creates a new row in RadGridView assuming that the first column is GridViewTextBoxColumn. You can extend this example and use a custom data object which can contain all the necessary data to be transferred (not just the node text).

As to the drag and drop framework, we decided that it is not essential to implement one yet since we have other priorities(code base stability and documentation) and that the standard drag and drop mechanism is easy enough to use. Write again if you need further assistance.

Sincerely yours,
Victor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jay
Top achievements
Rank 1
answered on 09 Oct 2009, 07:19 PM
Sorry for the late reply but was just able to get back onto this project.

it works great!

thanks for the posted code.

Jay
0
John Benny
Top achievements
Rank 1
answered on 13 Nov 2009, 09:02 PM
hi

I am writing C# win form.
I have created user control for RadGrid and created one more usercontrol for RadTreeview.  I am using both these
user control in my form. I need to drag item from grid to Tree view. How to do this? and How to raise these events.
Please let me kow.

regards,
John
0
Nikolay
Telerik team
answered on 24 Nov 2009, 04:24 PM
Hi John Benny,

Could you please elaborate a bit more on the issues that you encounter? I am not sure that I undestand your question about raising the events.

You can refer to the solutions that my colleagues Jordan and Victor have given in this forum thread. These snippets are tested and as you can see they are working for our clients. So, it should be something specific in your case that prevents you from using these solutions.

Sincerely yours,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
John Benny
Top achievements
Rank 1
answered on 24 Nov 2009, 07:16 PM
hi,

Thanks for your response.

Sorry, it is my mistake. After i removed couple of code in my application, events is working fine.

Thanks,

John
Tags
Treeview
Asked by
Richard Thurgood
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Richard Thurgood
Top achievements
Rank 1
Ilya
Top achievements
Rank 1
Jure
Top achievements
Rank 1
Jay
Top achievements
Rank 1
Victor
Telerik team
John Benny
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or