Count checked node in treeview

6 posts, 0 answers
  1. Sperduti
    Sperduti avatar
    16 posts
    Member since:
    Oct 2007

    Posted 03 Oct 2007 Link to this post

    Hi, how can I do to count every checked nodes in a radtrreview in C# ? Thanks.
  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 05 Oct 2007 Link to this post

    Hello Sperduti,

    I've prepared a sample method which traverses an instance of RadTreeView and counts the checked nodes. You just need to pass an instance of RadTreeNodeCollection as its argument and it will return an integer value representing the checked  nodes:
     
    public int GetCheckedNodesCount(Telerik.WinControls.UI.RadTreeNodeCollection nodes)  
            {  
                int checkedNodes = 0;  
     
                for (int i = 0; i < nodes.Count; i++)  
                {  
                    RadTreeNode node = nodes[i];  
                    if (node.Checked)  
                        checkedNodes++;  
     
                    if (node.Nodes.Count > 0)  
                        checkedNodes += GetCheckedNodesCount(node.Nodes);  
                }  
     
                return checkedNodes;  
     
            } 

    I hope this helps. Please tell me If you need further assistance.

     
    Best wishes,
    Boyko Markov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tony
    Tony avatar
    14 posts
    Member since:
    Jul 2008

    Posted 29 Jan 2009 Link to this post

    This is the exact issue I was trying to get an answer for.  However, when I try to create the 'an instance of RadTreeNodeCollection' I am running into an issue that it only seems to recognize the root node.

    How would I go about getting a collection of all the nodes in the treeview?

    Thank you
  5. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 30 Jan 2009 Link to this post

    Hi Tony,

    Did you try to use the nodes collection of the TreeView (the Nodes property of RadTreeView)?

    I hope this helps. Please contact me again if you have other questions.

    Sincerely yours,
    Boyko Markov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Tony
    Tony avatar
    14 posts
    Member since:
    Jul 2008

    Posted 30 Jan 2009 Link to this post

    Dim

     

    myCount As Integer = uxSecurityDisplay.Nodes(0).GetNodeCount(True)  --> Returns 176  (the number of nodes in the tree)

     

     

    Dim myValues1 As RadTreeNodeCollection = uxSecurityDisplay.Nodes   -->  Returns a collection of 1 (the root node)

     

     

     

    Dim myValues2 As RadTreeNodeCollection = uxSecurityDisplay.Nodes(0).Nodes  -->  Returns a collection of 15 (my first level nodes)

    How can I get a collection of 176 nodes?

     

  7. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 30 Jan 2009 Link to this post

    Hello Tony,

    We do not have a method which traverses all the nodes of RadTreeView and returns a collection of all of its nodes. The implementation however is pretty simple. You just need to implement a recursive method which traverses all tree nodes and the only parameter of this method should be an instance of RadTreeNodeCollection:

    public List<RadTreeNode> GetNodes(RadTreeNodeCollection nodes) 
            { 
                List<RadTreeNode> newNodes = new List<RadTreeNode>(); 
     
                foreach (RadTreeNode node in nodes) 
                { 
                    newNodes.Add(node); 
                    newNodes.AddRange(GetNodes(node.Nodes)); 
                } 
     
                return newNodes; 
            } 

    Public Function GetNodes(nodes As RadTreeNodeCollection) As List(Of RadTreeNode) 
        Dim newNodes As New List(Of RadTreeNode)() 
     
        For Each node As RadTreeNode In nodes 
            newNodes.Add(node) 
            newNodes.AddRange(GetNodes(node.Nodes)) 
        Next 
     
        Return newNodes 
    End Function 

    And then you can use the method:

      List<RadTreeNode> newNodes = GetNodes(this.radTreeView1.Nodes); 

    Dim newNodes As List(Of RadTreeNode) = GetNodes(Me.radTreeView1.Nodes) 

    I hope this helps. Do not hesitate to contact me back if you need further assistance.

    Regards,
    Boyko Markov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for WinForms is Visual Studio 2017 Ready