On-Demand Loading of Winform Treeview, winformtreeview

Source: Internet
Author: User

On-Demand Loading of Winform Treeview, winformtreeview

Recently, treeview was used in the project. It was originally designed to load all the data to the treeview at the beginning. Later, it was found that the customer's data volume was too large and it took 2 minutes to load all the data, this is unacceptable to customers. Later, considering that users do not have to view all the data at the beginning, users also expand layer by layer, therefore, we should consider whether it is possible to load the data below the current node only when the user expands a node. After some searches, we found that the treeview has the BeforeExpand event to meet our needs.

The following describes the specific implementation code:

1. First, only the information of each department (node) is loaded.

 

1: List <string> m_ments ments = new List <string> () {"Hubei. Huangshi", "Hubei. Ezhou", "Hubei. Wuhan "};
  2: private void AddDepartMents(List<string> departments)
  3: {
  4:     if (m_Root == null)
  5:     {
  6:         var root = departments[0].Split('.')[0];
  7:         m_Root = new TreeNode(root);
  8:         m_Root.Tag = root;
  9:         treeView1.Nodes.Add(m_Root);
 10:     }
 11: 
 12: 
 13:     foreach (var department in departments)
 14:     {
 15:         var parent = m_Root;
 16:         var dts = department.Split('.');
 17:         for (int i = 1; i < dts.Length; i++)
 18:         {
 19:             if (!m_OrgNodeManager.ContainsKey(dts[i]))
 20:             {
 21:                 var child = new TreeNode(dts[i],1,1);
 22:                 child.Tag = dts[i];
 23:                 child.ToolTipText = department;
 24:                 m_OrgNodeManager.Add(dts[i], child);
 25:                 parent.Nodes.Add(child);
 26:                 parent = child;
 27:             }
 28:         }
 29: 
 30:         parent.Nodes.Add("");
 31:     }
 32: }

 

Note that after each node is added, an empty sub-node parent. Nodes. Add ("") must be added; otherwise, the plus sign will not be available for you to click.

 

2. Implement the BeforeExpand event

  1: private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  2: {
  3:     TreeNode curentNode = e.Node;
  4:     if (curentNode.ImageIndex == 1)
  5:     {
  6:         curentNode.Nodes.Clear();
  7:         foreach (var user in m_UserManager)
  8:         {
  9:             if (user.Value.ToString() == curentNode.ToolTipText.ToString())
 10:             {
 11:                 TreeNode userNode = new TreeNode(user.Key);
 12:                 curentNode.Nodes.Add(userNode);
 13:             }
 14:         }
 15:     }
 16: }

C # In winform development, if too many treeview nodes lead to slow loading, how can we provide efficiency?

You can choose to attach and load the data of the parent node first, and then load the data of the child node when you click the expand button.

C # How can I improve the efficiency of winform development when treeview dynamically loads a large number of nodes (1 million?

It is impossible for you to load and display the 1 million nodes at a time, load the data to be displayed. (For example, you can only display 20 data items at a time on the screen. You can only read and create the 20 data items ). If you need to load a large amount of data at a time, you can consider it like multithreading. If you have a long wait time, you also need to do things like progress bars.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.