winform treeview 添加父节点和子节点

public void AddNode(MatchTypeList matchTypeList)
        {
            //添加父节点
            if (!treeView1.Nodes.ContainsKey(matchTypeList.ParentId))//先判断是否已经存在这个节点
            {
                TreeNode root = new TreeNode();//创建节点
                root.Name = matchTypeList.ParentId;
                root.Text = matchTypeList.ParentName;
                treeView1.Nodes.Add(root);//将节点添加到treeView1上
            }
            //添加子节点
            var root1=treeView1.Nodes.Find(matchTypeList.ParentId, false).FirstOrDefault();//先判断是否已经存在这个根节点
            if (!root1.Nodes.ContainsKey(matchTypeList.MatchTypeId))
            {
                TreeNode node = new TreeNode();//创建节点
                node.Name = matchTypeList.MatchTypeId;
                node.Text = matchTypeList.MatchTypeName;
                root1.Nodes.Add(node);//将子节点添加到根节点上
            }
        }


版权声明:本文为timsqin原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。