All my TreeView nodes have a unique ID for their Node Depth.
I want to set Checked=True
on the TreeView Node which matches a certain value.
Currently I'm 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 of finding the Node which I want to set as Checked=True
rather than looping through each node?
I'm looking for something like:
Dim value As Integer = 57
n.FindNodesByValue(value开发者_如何学编程)(0).Checked = True
Is there anything like this that I can use?
Pseudocode (c#
) to demonstrate an idea using LINQ Where() + List.ForEach():
nodes.Where(node => node.Value == "5")
.ToList()
.ForEach((node => node.Checked = true));
See MSDN
following the links above for VB.NET
syntax of both methods.
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;
}
}
}
for (int j = 0; j < TreeView1.CheckedNodes.Count; j++)
{
Response.Write(TreeView1.CheckedNodes[j].Value));
}
精彩评论