This is a migrated thread and some comments may be shown as answers.

Count the number of ALL child nodes and display it on the parent node

2 Answers 1542 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Borralis
Top achievements
Rank 1
Borralis asked on 14 Mar 2012, 04:56 PM
Hello,

Is it possible to count the number of child nodes of a node and display it on the parent node?

I tried this: http://www.telerik.com/help/aspnet/treeview/tree_show_child_count.html, but it only counts the number of the first level child nodes… What I need is to count all level nodes and display it next to the parent node text. Somehing like this:
-root (8)
---node_1 (4)
------node_1.1 (2)
----------node_1.1.1 (0)
----------node1.1.2 (0)
------node_1.2 (0)
---node_2 (4)
------node_2.1 (0)
------node_2.2 (1)
----------node_2.2.1 (0)
------node_2.3 (0)

How can I make this?

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Bozhidar
Telerik team
answered on 15 Mar 2012, 01:47 PM
Hi Nabo,

The Nodes property of RadTreeNode returns only the direct children of a particular node. If you want to get all of the child nodes, you can use the GetAllNodes() function.

In your case to display the number of child nodes next to the text of the node, you can use the DataBound event:
protected void RadTreeView1_DataBound(object sender, EventArgs e)
{
    foreach (RadTreeNode node in RadTreeView1.GetAllNodes())
    {
        node.Text += " (" + node.GetAllNodes().Count.ToString() + ")";
    }
}
 
All the best,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Borralis
Top achievements
Rank 1
answered on 15 Mar 2012, 03:32 PM

Thanks Bozhidar,

That was exactly what I was looking for.

Tags
TreeView
Asked by
Borralis
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Borralis
Top achievements
Rank 1
Share this question
or