缓慢加载treeview

时间:2014-09-12 08:50:26

标签: c# performance list treeview loading

我目前很难加载Treeview。我的应用程序有一个中央面板,其中加载了多个用户控件。每个人都有5个父节点(组)的树视图,拥有数十个子节点(人),共有50个节点。 相同的功能加载树视图(见下文): 第一个循环是添加组,第二个循环(在第一个循环中)添加人员。

该功能大约需要10秒钟才能加载50个节点。这是我世界的永恒。

不知道如何加强这个。请看一下这个功能。

    public static List<TreeNode> ArbreCommun(Guid idUser)
    {
        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

        stopWatch.Start();


        List<TreeNode> ArbreParent = new List<TreeNode>();

        using (bdd_compactEntities contexte = new bdd_compactEntities())
        {
            if (contexte.Groupe.Count() > 0)
                foreach (Groupe grpItem in contexte.Groupe)
                {
                    if (grpItem.g_nom == "Tous")
                    {
                        TreeNode Tous = new TreeNode();
                        Tous.Text = grpItem.g_nom + " (" + contexte.Personne.Count() + ")";
                        Tous.Tag = grpItem.id;
                        Tous.ToolTipText = "ID groupe = " + grpItem.id;
                        Tous.ImageKey = "group";
                        Tous.SelectedImageKey = "group";

                        foreach (Personne pers in FonctionsYou.ChargementPersonnesTous())
                        {
                            TreeNode PersonneNode = new TreeNode();
                            PersonneNode.Text = pers.p_nom + " " + pers.p_prenom;
                            PersonneNode.Tag = pers.id;
                            PersonneNode.ToolTipText = "ID personne = " + pers.id;

                            if (FonctionsYou.HasGroupe(idUser, FonctionsYou.GetGroupeIdPers(pers.id)))
                            {
                                if (FonctionsYou.CheckDisponibiliteAthlete(pers.id))
                                {
                                    PersonneNode.ForeColor = Color.Blue;
                                    PersonneNode.ImageKey = "dispo";
                                    PersonneNode.SelectedImageKey = "dispo";
                                }
                                else
                                {
                                    PersonneNode.ForeColor = Color.Red;
                                    PersonneNode.ImageKey = "blesse";
                                    PersonneNode.SelectedImageKey = "blesse";
                                }
                            }

                            Tous.Nodes.Add(PersonneNode);
                        }

                        ArbreParent.Add(Tous);
                    }
                    else
                    {
                        TreeNode GroupeNode = new TreeNode();
                        GroupeNode.Text = grpItem.g_nom + " (" + grpItem.Personne.Count() + ")";
                        GroupeNode.Tag = grpItem.id;
                        GroupeNode.ToolTipText = "ID groupe = " + grpItem.id;
                        GroupeNode.ImageKey = "group";
                        GroupeNode.SelectedImageKey = "group";
                        if (FonctionsYou.HasGroupe(idUser, grpItem.id))
                        {
                            GroupeNode.ForeColor = Color.Green;
                        }

                        foreach (Personne pers in contexte.Personne.Where(x => x.Groupe_id == grpItem.id))
                        {
                            TreeNode PersonneNode = new TreeNode();
                            PersonneNode.Text = pers.p_nom + " " + pers.p_prenom;
                            PersonneNode.Tag = pers.id;
                            PersonneNode.ToolTipText = "ID personne = " + pers.id;

                            if (FonctionsYou.HasGroupe(idUser, FonctionsYou.GetGroupeIdPers(pers.id)))
                            {
                                if (FonctionsYou.CheckDisponibiliteAthlete(pers.id))
                                {
                                    PersonneNode.ForeColor = Color.Blue;
                                    PersonneNode.ImageKey = "dispo";
                                    PersonneNode.SelectedImageKey = "dispo";
                                }
                                else
                                {
                                    PersonneNode.ForeColor = Color.Red;
                                    PersonneNode.ImageKey = "blesse";
                                    PersonneNode.SelectedImageKey = "blesse";
                                }
                            }

                            GroupeNode.Nodes.Add(PersonneNode);
                        }
                        ArbreParent.Add(GroupeNode);

                    }
                }
        }
        stopWatch.Stop();
        MessageBox.Show(stopWatch.ElapsedMilliseconds + " ms");
        return ArbreParent;
    }

1 个答案:

答案 0 :(得分:2)

顺便说一下,这是我的调用方法:

            List<TreeNode> _itemsNodes = FonctionsYou.ArbreCommun(userConnecte.uid);
            treeview_arbre.ImageList = FonctionsYou.GetImageListTV();
            // Display a wait cursor while the TreeNodes are being created.
            Cursor.Current = Cursors.WaitCursor;

            // Suppress repainting the TreeView until all the objects have been created.
            treeview_arbre.BeginUpdate();

            foreach (TreeNode node in _itemsNodes)
            {
                treeview_arbre.Nodes.Add(node);
            }

            // Reset the cursor to the default for all controls.
            Cursor.Current = Cursors.Default;

            // Begin repainting the TreeView.
            treeview_arbre.EndUpdate();