Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow treeview in C#

I have a legacy application that is written in C# and it displays a very complex treeview with 10 to 20 thousand elements.

In the past I encountered a similar problem (but in C++) that i solved with the OWNERDATA capability offered by the Win32 API.

Is there a similar mechanism in C#?

EDIT: The plan is to optimize the creation time as well as browsing time. The method available through Win32 API is excellent in both of these cases as it reduce initialization time to nothing and the number of requests for elements are limited to only the ones visible at any one time. Joshl: We are actually doing exactly what you suggest already, but we still need more efficiency.

like image 355
Fabien Hure Avatar asked Sep 23 '08 13:09

Fabien Hure


2 Answers

One technique for improving performance is to load TreeNodes as the user expands the treeview. Normally a user will not require 20,000 nodes to be open on their screen at once. Only load the level that the user needs to see, along with whatever child information you need to properly display affordances to the user (expand icon if children exist, counts, icons, etc). As the user expands nodes, load children just in time.

Helpful hint from Keith: With the winforms TreeView you need to have at least one child node or it won't show the expand [+], but then you handle the TreeNodeExpanded event to remove that dummy node and populate the children.

like image 145
JoshL Avatar answered Sep 23 '22 06:09

JoshL


In our main WinForm app, we have a treeview loaded all in one shot:

  • BeginUpdate()
  • Load 20.000 nodes
  • EndUpdate()

and so far the performance is still nice. It is actually one of the few components we are not replacing with third party ones.

The TreeView performance, in my experience, gets slow when you load nodes (in one shot, or on demand) without calling Begin/EndUpdate(), especially if your nodes are sorted, but if you call Begin/EndUpdate() correctly, you shouldn't really get performance issues related to the component itself.

like image 33
Filini Avatar answered Sep 21 '22 06:09

Filini