问题描述
ROOT
A
B
C
D
E
T
F
G
X
我要查找电子节点的父节点(这是5号)。然后,我可以节省节点。如果数量较少5.我使用在Asp.net控制树视图。
I want to find E Node's parent nodes(it is number 5). Then, I'll 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.
因此,这个例子中,你可以传递E和期望有则E返回干脆节点
如果返回节点的parent属性将是家长,你是后。
Thus, with this example you could pass in E and expect to have the node E returned then simplyif the parent property of the node returned would be the parent you are after.
tn treenode = FindNode(myTreeview, "E")
tn.parent
是价值你了。
这篇关于如何找到根节点的子节点[TreeView控件]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!