ROOT
A
B
C
D
E
T
F
G
X
i want to find E Node's parent nodes(it is number 5).then i will save node, if number is smaller 5. i'm using TreeView in Asp.net control.
ROOT
A
B
C
D
E
T
F
G
X
i want to find E Node's parent nodes(it is number 5).then i will save node, if number is smaller 5. i'm using TreeView in Asp.net control.
I would suggest using recursive iterations.
private TreeNode FindNode(TreeView tvSelection, string matchText)
{
foreach (TreeNode node in tvSelection.Nodes)
{
if (node.Tag.ToString() == matchText)
{
return node;
}
else
{
TreeNode nodeChild = FindChildNode (node, matchText);
if (nodeChild != null) return nodeChild;
}
}
return (TreeNode)null;
}
You can utilize this logic to determine many things about you node and this structure also allows you to expand what you can do with the node and the criteria you wish to search for. You can edit my example to fit your own needs.
Thus, with this example you could pass in E and expect to have the node E returned then simply if the parent property of the node returned would be the parent you are after.
tn treenode = FindNode(myTreeview, "E")
tn.parent
is the value you are after.
What is your datasource for the treeview? let's suppose you have implemented a custome datasouce model.
like below
public class Node { public string NodeName { get; set; }
public Node Parent { get; set; }
public List<Node> Childs { get; set; }
}
So create a function which will traverse node's parent untill found a node which parent is null.
private void BuildNodePath(Node childNode)
{
Stack<Node> ParentNodes = new Stack<Node>();
Node tempNode = childNode;
while (tempNode.Parent != null)
{
ParentNodes.Push(childNode.Parent);
tempNode = childNode.Parent;
}
}