树视图的第一项为空

时间:2017-01-02 10:49:00

标签: c# .net winforms treeview

我有一个线程将填充TreeNode集合以不阻止主线程(窗体)。所以我通过这些行调用TreeView.Nodes.Add命令:

private void generateListeItems()
        {
            try
            {
                Item_Coll resultSet = connector.command_LIST();
                if(mainFormInvoke != null)
                    mainFormInvoke(formProgressBar, -1);
                TreeNode racine = new TreeNode();
                TreeNode NET = racine.Nodes.Add("Net");
                TreeNode LON = NET.Nodes.Add("LON");
                List<string> devices = new List<string>();

                TreeNode lastDevice = null;
                int lastDevice_index = -1;
                TreeNode lastDevice_fb = null;
                int lastDevice_fb_index = -1;
                int percentage = 0, lastPercentage = 0, treatedItem = 0;
                int nombreIndex = resultSet.Item.Count<Item>();
                foreach (Item index in resultSet.Item)
                {
                    string[] substring = index.UCPTname.Split('/');
                    if (lastDevice == null || substring[2] != lastDevice.Text)
                    {
                        lastDevice = LON.Nodes.Add(substring[2]);
                        lastDevice_index++;
                        lastDevice_fb_index = -1;
                    }

                    if (lastDevice_fb == null || substring[3] != lastDevice_fb.Text)
                    {
                        lastDevice_fb = lastDevice.Nodes.Add(substring[3]);
                        lastDevice_fb_index++;
                    }

                    lastDevice_fb.Nodes.Add(substring[4]);
                    dpDictionnary[index.UCPTname] = index;
                    lastPercentage = percentage;
                    percentage = ++treatedItem * (nombreIndex / 100);
                    if(percentage != lastPercentage)
                        mainFormInvoke(formProgressBar, 1);
                }

                mainFormInvoke((updatingListeInUI)liste.Nodes.Add, racine);
                mainFormInvoke(consoleWriter,string.Format("Connexion réussie, récupération de {0} éléments", resultSet.Item.Count<Item>()));
            }catch(SmartServerException e)
            {
                mainFormInvoke(consoleWriter, e.ToString());
            }

        }

我得到了这个:

enter image description here

我不想要第一个null元素。我怎么能通过这个?

1 个答案:

答案 0 :(得分:0)

TreeNode racine = new TreeNode("Root");
TreeNode NET = racine.Nodes.Add("Net");
TreeNode LON = NET.Nodes.Add("LON");

将默认节点设置为root,如上所示