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

Reorder Tree nodes help

12 Answers 273 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Mark asked on 01 Sep 2016, 06:30 PM

I still learning a lot about C# and Telerik. I am trying to figure out what NODE I am moving in a TREEVIEW when I am done moving it.  Any node in my tree can move and any node can be in any position (ROOT or CHILD). I have looked at DragEnded event, but the sender is never the node I have selected and/or moved.I have looked at SelectedNodeChanged, but that fires multiple times during a drag event and while the last one is the correct node, I don't believe this is the correct direction.  As for DragDrop even, this never fires, which I found both weird and interesting, as I have both AllowDragDrop and AllowDrop turned on for the tree view. I am currently using Telerik Winforms version 2016.2.503.40.

 

Thanks in advanced.

 

Mark

12 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 02 Sep 2016, 01:24 PM
Hello Mark,

Thank you for writing.

The TreeView has it own DragDropService which is responsible for the drag and operation. In your case, you can use the PreviewDragDrop event:
public RadForm1()
{
    InitializeComponent();
    radTreeView1.AllowDragDrop = true;
    radTreeView1.TreeViewElement.DragDropService.PreviewDragDrop += DragDropService_PreviewDragDrop;
}
 
private void DragDropService_PreviewDragDrop(object sender, Telerik.WinControls.RadDropEventArgs e)
{
    var draggedNode = e.DragInstance as TreeNodeElement;
    Console.WriteLine(draggedNode.Data.Text);
}

I hope this will be useful. Let me know if you have additional questions.

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 02 Sep 2016, 02:33 PM
This was awesome, but how do I get the Key to the Node. Note I am using DRAG & DROP BOUND DATA tree view from the example here: http://docs.telerik.com/devtools/winforms/treeview/drag-and-drop/drag-and-drop-in-bound-mode
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 02 Sep 2016, 02:40 PM

Thanks for the help, I figured it out from the sample code in that link I posted..

 

0
Dimitar
Telerik team
answered on 05 Sep 2016, 12:25 PM
Hi Mark,

I am glad that you have found a solution for your case. Do not hesitate to contact us if you have other questions.
 
Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 07 Sep 2016, 09:01 PM

Dimitar:

I have almost everything working, made a lot of changes to use a List<> vs DT, which made it a lot cleaner.  However, I have a question, We are using the RadBreadCrumb as well.  The problem is when we drag/drop and we get into the OnPreviewDragDrop of the CustomDragDropService, it crashes when we set the DataSource of the treeview to NULL, and that's because the RadBreadCrumb.DefaultTreeView is referencing the treeview, which now has no data.  Is there a way around this? We really would like to use the RadBreadCrumb object.  I have tried the "DragStarted" and "DragEnded" events, but the DragEnded event never fires.

Thanks

0
Dimitar
Telerik team
answered on 08 Sep 2016, 08:42 AM
Hello Mark,

You should set the DefaultTreeView to null before changing the data source:
radBreadCrumb1.DefaultTreeView = null;
  
radTreeView1.DataSource = null;
radTreeView1.DataSource = dataItems;
 
radBreadCrumb1.DefaultTreeView = radTreeView1;

Let me know if I can assist you further.

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 08 Sep 2016, 11:26 AM
Yea, I know those are the lines of code I need, however, the DataSource is being set in the DragDrop Service (see your example here: drag-and-drop-in-bound-mode).  radBreadCrumb is not available in the service. 
0
Dimitar
Telerik team
answered on 08 Sep 2016, 12:13 PM
Hi Mark,

You can create a property in your CustomDragDropService class:
class CustomDragDropService : TreeViewDragDropService
{
    private RadTreeViewElement owner;
    private List<RadTreeNode> draggedNodes;
    private RadBreadCrumb crumb;
 
    public RadBreadCrumb Crumb
    {
        get
        {
            return this.crumb;
        }
        set
        {
            this.crumb = value;
        }
    }
 
 
    public CustomDragDropService(RadTreeViewElement owner) : base(owner)
    {
        this.owner = owner;
        this.crumb = crumb;
    }
}

Then you can set it like this:
((CustomDragDropService)radTreeView1.TreeViewElement.DragDropService).Crumb = radBreadCrumb1;

Let me know if I can assist you further.

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 08 Sep 2016, 12:31 PM

Thanks, this was helpful.  I did run into one other issue in the DragDropService, and that was know the tree view that service belonged too.  I could not use "owner" as that was a treeviewelement object, so I did what you did with the BreadCrumb object.  Was that the correct direction?

 

Thanks

0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 08 Sep 2016, 12:42 PM
WOW, even after I re-read that, I was not able to comprehend what I was trying to say.  But my thought was to add a similar object for the treeview in question, but after thinking about it, I have the treeview I need, it's in the breadcrumb's DefaultTreeView object. So I can just get a reference of that and then use that reference when I am ready to re-assign crumb.DefaultTreeView.  Again, thanks for the help.
0
Accepted
Dimitar
Telerik team
answered on 12 Sep 2016, 09:02 AM
Hello Mark,

Here is an easier way to get the parent TreeView:
protected override void OnPreviewDragDrop(RadDropEventArgs e)
{
    base.OnPreviewDragDrop(e);
    this.crumb.DefaultTreeView = null;
 
    this.crumb.DefaultTreeView = owner.ElementTree.Control as RadTreeView;
 
}

Please let me know if there is something else I can help you with. 

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
answered on 12 Sep 2016, 01:51 PM
Thanks so much for the help.
Tags
Treeview
Asked by
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Answers by
Dimitar
Telerik team
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Share this question
or