Find TreeView Node by Value

All of my TreeView nodes have a unique identifier for Node depth.

I want to set Checked=Truea Node in the TreeView that matches a specific value.

I am currently doing the following:

Dim value As Integer = 57

For Each n As TreeNode In tvForces.Nodes
   If n.Value = value Then n.Checked = True
Next

Is there a better way to find the Node that I want to set as Checked=True, rather than scrolling through each node?

I am looking for something like:

Dim value As Integer = 57

n.FindNodesByValue(value)(0).Checked = True

Is there anything similar that I can use?

+5
source share
3 answers

Pseudocode ( c#) to demonstrate the idea using LINQ Where () + List.ForEach ():

nodes.Where(node => node.Value == "5")
     .ToList()
     .ForEach((node => node.Checked = true));

. MSDN VB.NET .

+5
                foreach (TreeNode node in TreeView1.Nodes)
                {
                    if (node.Value == "8")
                    {
                        node.Checked = true;
                    }
                    foreach (TreeNode item1 in node.ChildNodes)
                    {
                        if (item1.Value == "8")
                        {
                            item1.Checked = true;
                        }
                    }
                }               
0
for (int j = 0; j < TreeView1.CheckedNodes.Count; j++)
    {    
        Response.Write(TreeView1.CheckedNodes[j].Value));
    }
-1
source

All Articles