Search code examples
c#asp.nettreeviewtreenode

Delete checked node in a Tree view c#


I have a tree with multiple nodes with different depth. So I need to use the check boxes for deleting nodes not the selection method.


Solution

  • I have tried this code and it worked good, if anyone have a comment please come up with it.

        ArrayList checkedNodes = new ArrayList();
    
        if (elementsHierTree.CheckedNodes.Count != 0)
        {
            foreach (TreeNode nodee in elementsHierTree.CheckedNodes)
            {
                if (nodee.Parent != null)
                {
                    checkedNodes.Add(nodee);
                }
            }
        }
    
        foreach (TreeNode chNode in checkedNodes)
        {
            chNode.Parent.ChildNodes.Remove(chNode);
        }