开发者

Treeview Refresh Questions

开发者 https://www.devze.com 2023-02-28 22:29 出处:网络
I have a Treeview that the user cannot edit. The way I\'m trying to refresh is clearing the entire tree and re-adding all the nodes (and children). I\'m accomplishing this by the following lines of co

I have a Treeview that the user cannot edit. The way I'm trying to refresh is clearing the entire tree and re-adding all the nodes (and children). I'm accomplishing this by the following lines of code:

treeView.BeginUpdate(); //Freeze drawing
treeView.Nodes.Clear(); //Empty Tree
addAllNodes();  //This adds the nodes for the tree and sets their name/text property
treeView.EndUpdate(); //Unfreeze drawing

I've tried adding the Update and Refresh method before I call开发者_高级运维 addAllNodes but hasn't made a difference. Doing the above gets me an error:

System.ArgumentException: Cannot add or insert the item 'NodeNameHere' in more than one place. You must first remove it from its current location or clone it

My first question is, what am I doing to cause this error and how can I properly refresh my tree?

My second question is, after the refresh is there any way I can restore the user's expanded nodes? (so that everything does not end up collapsed)


each node has a path (called something like .Path or .FullPath; dont have VS open right now). So you can walk your nodes before you clear them and record all the expanded nodes, then walk them again after refresh and see if the node's path matches a stored one.. if it does, expand it.

0

精彩评论

暂无评论...
验证码 换一张
取 消