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

3 posts, 1 answers
  1. Borralis
    Borralis avatar
    5 posts
    Member since:
    Dec 2011

    Posted 14 Mar 2012 Link to this post

    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. Answer
    Bozhidar
    Admin
    Bozhidar avatar
    1103 posts

    Posted 15 Mar 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Borralis
    Borralis avatar
    5 posts
    Member since:
    Dec 2011

    Posted 15 Mar 2012 Link to this post

    Thanks Bozhidar,

    That was exactly what I was looking for.

Back to Top