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

Count checked node in treeview

5 Answers 817 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Sperduti
Top achievements
Rank 1
Sperduti asked on 03 Oct 2007, 01:13 PM
Hi, how can I do to count every checked nodes in a radtrreview in C# ? Thanks.

5 Answers, 1 is accepted

Sort by
0
Boyko Markov
Telerik team
answered on 05 Oct 2007, 05:09 PM
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
0
Tony
Top achievements
Rank 1
answered on 29 Jan 2009, 11:31 PM
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
0
Boyko Markov
Telerik team
answered on 30 Jan 2009, 08:01 AM
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.
0
Tony
Top achievements
Rank 1
answered on 30 Jan 2009, 01:23 PM

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?

 

0
Boyko Markov
Telerik team
answered on 30 Jan 2009, 02:18 PM
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.
Tags
Treeview
Asked by
Sperduti
Top achievements
Rank 1
Answers by
Boyko Markov
Telerik team
Tony
Top achievements
Rank 1
Share this question
or