winform treeview 添加父节点和子节点

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);//将子节点添加到根节点上

}

}

相关推荐

诗人的采撷|那些生长在诗歌里的造物——《绿竹》
qq阅读器怎样收藏书 收藏书籍的方法介绍
365提款经验

qq阅读器怎样收藏书 收藏书籍的方法介绍

📅 07-04 👁️ 8747
新赛道,新征程!2023NEST全国电子体育大赛即将开启!