欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

我的treeview动态添加节点方法

程序员文章站 2022-06-08 09:16:46
...

调用方法

            this.TreeView.Nodes.Clear();
            TreeNode MainNode = new TreeNode();
            MainNode.Text = "总分类";
            MainNode.Name = "000";
            SetTree(listProfe.ToList(), MainNode.Name,MainNode);
            this.TreeView.Nodes.Add(MainNode);

将节点name设定为实体的Guid或者Id唯一性质的,将Text设定为实体的Name

    public  void SetTree(List<TrainingProfe> list,string Flag,TreeNode inNode)
    {
        List<TrainingProfe> listSe = new List<TrainingProfe>();

        listSe = list.Where(x => x.PGuid == Flag).ToList();///先判断是否有,然后再添加
        if (listSe.Any())
        {
            foreach (TrainingProfe one in list)
            {

                TreeNode oneNode = new TreeNode();
                oneNode.Text = one.Name;
                oneNode.Name = one.Guid;
                oneNode.Tag = one;

                //oneNode.Nodes.Add(SetTree(listSe,one.Guid,oneNode));
                SetTree(list, oneNode.Name, oneNode);
                oneNode.Checked = one.IsUsed;
                inNode.Nodes.Add(oneNode);
            }
        }

    }
相关标签: 关于程序