how to find child nodes at root node [TreeView]

 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’ll save node. If number is smaller 5. I’m using TreeView in Asp.net control.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

    private TreeNode GetNode(string key)
    {
        TreeNode n = null ;
        n = GetNode(key, Tree.Nodes);
        return n;
    }
    private TreeNode GetNode(string key,TreeNodeCollection nodes)
    {
        TreeNode n = null;
        if (nodes.ContainsKey(key))
            n = nodes[key];
        else
        {
            foreach (TreeNode tn in nodes)
            {
                n = GetNode(key, tn.Nodes);
                if (n != null) break;
            }
        }

        return n;
    }

Method 3

I’m curious, since this is tagged as a WebForm, why Microsoft’s FindNode method was not suggested. It is compatible from v2.0 up to now (currently v4.5.2).

Does that not work here?

From Microsoft’s MSDN:

Use the FindNode method to get a node from the TreeView control at the specified value path. The value path contains a delimiter-separated list of node values that form a path from the root node to the current node. Each node stores its value path in the ValuePath property. The PathSeparator property specifies the delimiter character that is used to separate the node values.

Example:

void Button_Click(Object sender, EventArgs e)
{

  // Find the node specified by the user.
  TreeNode node = LinksTreeView.FindNode(Server.HtmlEncode(ValuePathText.Text));

  if (node != null)
  {
    // Indicate that the node was found.
    Message.Text = "The specified node (" + node.ValuePath + ") was found.";
  }
  else
  {
    // Indicate that the node is not in the TreeView control.
    Message.Text = "The specified node (" + ValuePathText.Text + ") is not in this TreeView control.";
  }

}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x