I have a treeview that displays certain information. If specific information needs to be flagged, the node is coloured in red. The user can view a datagrid displaying all information pertaining to the red nodes, on a button click event.
Each datagridrow has a checkbox that can be used to toggle the flag on the treenode, which updates the sql database flag. What I also want to do is toggle the colour of the node (ie, the visual flag), back to black. I know how to do this using recursion, however the treeview may grow to hold 100's or 1000's of nodes, and I am worried about efficiency. 开发者_开发百科Is there any other way to interact with a specific node, without having to recurse?
Thanks.
In General , if your tree exhibits some property like Binary tree or BST , you can reduce the time but that is not the case with winforms treeview scenarios most of the time.
so there is no other way to reach to the actual node but to recurse the tree.
But you can make use of asynchronous programming to search on the secondary thread.
An alternative could be storing a foreach GridRow
, a reference to the coresponding TreeNode
.
Here you have two options:
- If you know the
TreeNode
reference. It's very easy. All it's done. - I guess that you don't know the
TreeNode
reference from theGrid
... In this case you can save aString
reference to the node "full path". For example:/A/B/C/D
If you know the full path of the node, you can search on the tree with efficiency.
Using this alternative, your efficiency searching the node will be better, but the memory consumption will be higher. If you implement it, please, monitor how your memory grows, specially if you have very long names in nodes.
Hope it helps.
精彩评论