Hi Reynard,
The problem you met is very common. I have met the same issue when I bind some data from database to a tree view. As far as I know, there is no direct way to speed this process. But we can change the binding way to reduce the count of the adding nodes when the tree view is first loaded. We only need to add the visible nodes and the children of these nodes. When one visible node is expanded, their children would be visible, then we begin to add nodes to their children.
These are the detail steps to add nodes to the tree view when it is loaded at the first time:
1. Save the DomDocument to a variable so that we can access it later.
2. Add the root nodes whose children are added to the tree view. The reason why we add the root nodes is because they ought to be visible. The reason why we add the children of the root nodes is because the root node need to show an ��tag if it has children.
We also need to handle BeforeExpand event to add nodes to the children of the expand node. We need to record the expand state of the added node to avoid duplicated adding. This is a code snippet:
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
foreach(TreeNode childNode in e.Node.Nodes)
{
//check if the nodes of childNode is added.
if (childNode.Tag == null || (bool)childNode.Tag == false)
{
//TODO:Add nodes to childNode here
//Save the state to indicate the nodes of childNode is already added.
childNode.Tag = true;
}
}
}
Please pay attention to this line //TODO:Add nodes to childNode here , you can write code about adding nodes here.
By the way, although the TreeView in WinForm does not support data binding, but the TreeView in WPF supports it. Since I am not familiar with WPF, I cannot give you more advice about it. You can ask for help in WPF forum.
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.