Reorder Tree nodes help

13 posts, 1 answers
  1. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 01 Sep Link to this post

    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

  2. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 02 Sep Link to this post

    Hello Mark,

    Thank you for writing.

    The TreeView has it own DragDropService which is responsible for the drag and drop 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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 02 Sep in reply to Dimitar Link to this post

    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
  5. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 02 Sep Link to this post

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

     

  6. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 05 Sep Link to this post

    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.
  7. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 07 Sep in reply to Dimitar Link to this post

    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

  8. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 08 Sep Link to this post

    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.
  9. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 08 Sep in reply to Dimitar Link to this post

    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. 
  10. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 08 Sep Link to this post

    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.
  11. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 08 Sep in reply to Dimitar Link to this post

    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

  12. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 08 Sep in reply to Mark Link to this post

    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.
  13. Answer
    Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 12 Sep Link to this post

    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.
  14. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 12 Sep in reply to Dimitar Link to this post

    Thanks so much for the help.
Back to Top
UI for WinForms is Visual Studio 2017 Ready