[SOLVED] TreeView drag and drop nodes with children

Answered Closed
0
0

Hi,

I’m moving TreeNodes in a treeview with drag and drop. It works for a single node, but not for a node with children on the next level.

The node I want to be moved changes it’s location, it still shows the icon of a parent-node (and is expandable) but shows no children.

treenode.Nodes.Count is like 3 for this node and there are all the children like before the drag operation (and have Parent set properly to my top node that has been moved) but they all have Level-Property set to zero instead of the correct level and are not being shown in treeview.

Any idea what I am missing or is it some kind of a bug? Cannot set level-prop on my own, there is no setter.

Thanks, Jan.

  • You must to post comments
Best Answer
0
0

Hi Jan Mika,

this is now fixed in our latest Wisej development build (2.1.48).

Best regards
Frank

  • You must to post comments
0
0

Hi Luca,

we already worked around with a short method which creates new nodes before removing the old branch.

Thanks,

Jan.

  • You must to post comments
0
0

The issue is indeed the call to Remove() it detaches the child nodes as well. Will log the bug. The temporary workaround is not to call Remove() and simply add to another node.

  • You must to post comments
0
0

Hi Luca,

I created a new testproject with a treeview and your handlers seem to work on the visible part but not in memory.

I have 3 parent nodes with 3 child nodes each:

Parent ONE – (children: 1, 2, 3)

Parent TWO – (children: 1, 2, 3)

Parent THREE – (children: 1, 2, 3)

 

Now I move Parent THREE let’s say to position Parent TWO(Child 3) as a child node, so afterwards we should have in treeview1.Nodes only two nodes left, but there are still nodes 0, 1, 2 in memory.

The problem is you do not remove the node at its old position before adding it again on the targetNode:

moveNode.Remove();
targetNode.Nodes.Add(moveNode);

But when I do this, I get exactly the behaviour I described. Under Parent TWO, Child 3 I have now Parent THREE with invisible children 1, 2, 3 (all have Level=0).

With this recursive function I iterate all the nodes (need it for saving to database) and the node that has been moved is being shown twice without being removed. If I remove it in the first place, this function shows them in the correct order and amount, but due to Level = 0 they do not appear in the treeview.

List<TreeNode> GetAllNodes(TreeNodeCollection nodes, List<TreeNode> list = null)
{
if (list == null) list = new List<TreeNode>();
if (nodes == null)
{
return list;
}

foreach (TreeNode node in nodes)
{
list.Add(node);
if ((node.Nodes != null) && (node.Nodes.Count > 0))
{
GetAllNodes(node.Nodes, list);
}
}
return list;
}

 

 

Thanks,

 

Jan

  • You must to post comments
0
0

I just tried and it seems to work well. Can you attach a sample to show how you move the node?

My code uses simply the code below (you need to select the node before dragging it):

 

private void treeView1_DragDrop(object sender, DragEventArgs e)
{
   var targetNode = e.DropTarget as TreeNode;
   if (targetNode != null)
  {
     var moveNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
     if (moveNode != null)
    {
      targetNode.Nodes.Add(moveNode);
   }
  }
 }

private void treeView1_DragStart(object sender, EventArgs e)
{
    this.treeView1.DoDragDrop(this.treeView1.SelectedNode, DragDropEffects.Move);
}

 

  • You must to post comments
Showing 5 results