I have a TreeView
Control with set of nodes and child nodes. For example:
ROOT has A,B,C.
A has a1, a2, a3 and then that a1, a2 also contains some nodes like x1, x2, x3 and so on. Like this many subnodes are there. I know it is possible to use loops with a for loop.
I just want to access all the nodes in TreeView
control using one or two for loops.
Is there any algorithm for that or is there any other way?
One more question: Is it is possible to have the path of a tree node in an object or in a string using any library functions? For example:
string S = TreeView1.Nodes[i].Nodes[j].Nodes
Solution: In a NodeSelectedEvent event, the event argument (e) holds the object of the selected node – Node. The selected node object have property called Items, it holds a list of child node object of the selected node. By iterating the Item object, you can get the information of each child node.
A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.
Populating TreeView from database Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is 0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the corresponding child nodes and again the PopulateTreeView method is called.
The key properties of the TreeView control are Nodes and SelectedNode. The Nodes property contains the list of top-level nodes in the tree view. The SelectedNode property sets the currently selected node.
Don't use nested loops, but go for an recursive solution like:
void ListNodes( TreeNode node )
{
foreach( var subnode in node.Nodes )
{
ListNodes( subnode );
}
// Print out node
}
Call this function for your root node.
For your additional question: check the FullPath
property.
You can use a recursive function to traverse the whole tree:
private void Traverse(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
Console.WriteLine("{0} -> {1}", node.Name, node.FullPath);
Traverse(node.Nodes);
}
}
You can then call this using:
Traverse(treeView.Nodes);
and it will walk the whole tree depth first (ie. going down as deep as it can before moving to the next sibling). Passing in the Nodes
collection means that this code will deal with trees that have multiple root nodes.
The example code above will print out the name of the node as well as the full path of that node within the tree.
I am not the biggest fan of recursion but it seems you must use it. I saw a clever example online mixing recursion with an iterator.
private int GetLevels(TreeNodeCollection treeNodes)
{
int level = 0;
foreach (TreeNode node in TreeTopDown(treeNodes))
{
int i = node.Level;
if (i > level) level = i;
}
return level;
}
//TopDown Iterator
private IEnumerable<TreeNode> TreeTopDown(TreeNodeCollection treeNodes)
{
foreach (TreeNode node in treeNodes)
{
yield return node;
foreach (TreeNode subNode in TreeTopDown(node.Nodes)) yield return subNode;
}
}
//BottomUp Iterator
private IEnumerable<TreeNode> TreeBottomUp(TreeNodeCollection treeNodes)
{
foreach (TreeNode node in treeNodes)
{
foreach (TreeNode subNode in TreeBottomUp(node.Nodes)) yield return subNode;
yield return node;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With