Question

I have a sorting function which accepts TreeVIew Node as argument and then sorts the node. How do I pass top node to this function?

Here's the code of my Tree View:

<asp:TreeView id="mytv" runat="server"></asp:TreeView>

Here's my sorting function code:

private void(TreeNode node)
{
   rest of code here
}

I tried the following but it didn't work.

sort(mytv.TopNode)

And

sort(mytv.Nodes)
Was it helpful?

Solution

Try this:

TreeNode currentNode = treeView.SelectedNode;
while (currentNode.Parent != null)
{
    currentNode = currentNode.Parent;
} 

You are iterating from some node (does not matter which one) and go up the hierarchy until the Parent of the current node is null, that is, the current node is the Root.

Also, here is the class reference for the TreeView:

TreeView class reference

You will be able to figure out, from the class reference that

sort(mytv.Nodes)

did not work because Nodes is not a node but rather a collection of nodes.

Also, the TreeView does not have a TopNode attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top