Page

C# TreeView Child Node Count

 // Iterate through the root nodes in the Nodes property.
        for (int i = 0; i < TreeView1.Nodes.Count; i++)
        {
            // Set the text to include children count.
            SetChildNodeText(TreeView1.Nodes[i]);
        }

 void SetChildNodeText(TreeNode node)
    {
        if (node.ChildNodes.Count >= 0)
        {
            node.Text += '(' + node.ChildNodes.Count.ToString() + ')'; // append child node count to the text
        }

        for (int i = 0; i < node.ChildNodes.Count; i++)
        {
            // recursion
            SetChildNodeText(node.ChildNodes[i]);
        }
    }

No comments:

Post a Comment