Search code examples
c#winformsuser-interfacetreeview

The best way to filter TreeView nodes


What's the best/efficient way to filter Treeview nodes?

For example: I typed "abc" and only the nodes contained "abc" become visible. Then I typed "abcd", and I should to see the only nodes containing "abcd" text. And so on, so each time I changed filter criterion the TreeView content also changes.

Any ideas?


Solution

  • If you are looking for the best performance, clone the tree, then remove all the items from the cloned tree, then simply replace the existing tree with the cloned (and filtered) one.

    I also keep a backing tree that is always unfiltered.

    This works well for me for quite big trees with 1000 - 2000 nodes.