C# Treeview checking if node exists

前端 未结 8 779
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 12:42

I\'m trying to populate a treeview from an XmlDocument. The Root of the tree is set as \'Scripts\' and from the root the next level should be \'Departments\' which is within the

8条回答
  •  我在风中等你
    2021-01-23 13:08

    You can do something like this:

    TreeNode parentNode = t1.Parent;
    if (parentNode != null}
    {
        if(parentNode.Nodes.Cast().ToList().Find(t => t.Text.Equals(node["DEPARTMENT"].InnerXml) == null)
        {
            //Add node
        }
    }
    else
    {
        bool isFound = true;
        if (treeView1.Nodes.Cast().ToList().Find(t => t.Text.Equals(node["DEPARTMENT"].InnerXml) == null)
        {
            isFound = false;
        }
    
        if(!isFound)
        {
            //Add node
        }
    }
    

提交回复
热议问题