6
votes
 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.

3
@Özkan: What number 4? Do you want to find the depth?Albin Sunnanbo
@Albin, yes, i want to find depth a node.ozkank
D is E's parent. D's parent is C.ozkank
You probably use some datasource to populate the treeview. What kind of datasource is it? You could walk the treeview and count the number of hops till you reach the root node - but using UI to implement business logic would be very bad.Goran
@Goran. i have TBLPRODUCTCATEGORYHIERARCHY table.there is 2 columns which name's are categoryId and parentCategoryId. Root Node's parentCategoryId's number is 0.ozkank

3 Answers

7
votes

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.

1
votes
    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;
    }
1
votes

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.";
  }

}