遍历数组以将每个项目添加到父节点
Looping through array to add each item to a parentNode
我有以下代码:
TreeNode parentNode1 = new TreeNode("CONNECTING RODS");
TreeViewNav.Nodes.Add(parentNode1);
string[] subNodes =
{
"STOCK", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(node);
}
所以我基本上是在尝试以更简洁的方式执行此操作:
TreeNode childNodeA = new TreeNode("A");
TreeNode childNodeB = new TreeNode("B");
TreeNode childNodeC = new TreeNode("C");
TreeNode childNodeD = new TreeNode("D");
TreeNode childNodeE = new TreeNode("E");
TreeNode childNodeF = new TreeNode("F");
parentNode1.ChildNodes.Add(childNodeA);
parentNode1.ChildNodes.Add(childNodeB);
parentNode1.ChildNodes.Add(childNodeC);
parentNode1.ChildNodes.Add(childNodeD);
parentNode1.ChildNodes.Add(childNodeE);
parentNode1.ChildNodes.Add(childNodeF);
我在 parentNode1.ChildNodes.Add(node);
行收到错误。
错误是
'string' is not assignable to paramenter type
'System.Web.UI.WebControls.TreeNode'
我知道它是因为我已将该数组设为字符串数组,但我不知道该怎么做。任何帮助将不胜感激:)
ChildNodes.Add
需要一个 TreeNode
对象,但你传递给它的是一个 string
。你应该:
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(new TreeNode(node));
}
关于添加子子节点:
foreach (var node in subNodes)
{
var treeNode = new TreeNode(node);
//Call function that returns all the sub-sub nodes
//Assign those nodes to 'treeNode' using another foreach - or better still have this as a recursive function
parentNode1.ChildNodes.Add(treeNode);
}
你应该使用 TreeNode 类型的字符串试试这个,
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(new TreeNode(node));
}
我有以下代码:
TreeNode parentNode1 = new TreeNode("CONNECTING RODS");
TreeViewNav.Nodes.Add(parentNode1);
string[] subNodes =
{
"STOCK", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(node);
}
所以我基本上是在尝试以更简洁的方式执行此操作:
TreeNode childNodeA = new TreeNode("A");
TreeNode childNodeB = new TreeNode("B");
TreeNode childNodeC = new TreeNode("C");
TreeNode childNodeD = new TreeNode("D");
TreeNode childNodeE = new TreeNode("E");
TreeNode childNodeF = new TreeNode("F");
parentNode1.ChildNodes.Add(childNodeA);
parentNode1.ChildNodes.Add(childNodeB);
parentNode1.ChildNodes.Add(childNodeC);
parentNode1.ChildNodes.Add(childNodeD);
parentNode1.ChildNodes.Add(childNodeE);
parentNode1.ChildNodes.Add(childNodeF);
我在 parentNode1.ChildNodes.Add(node);
行收到错误。
错误是
'string' is not assignable to paramenter type 'System.Web.UI.WebControls.TreeNode'
我知道它是因为我已将该数组设为字符串数组,但我不知道该怎么做。任何帮助将不胜感激:)
ChildNodes.Add
需要一个 TreeNode
对象,但你传递给它的是一个 string
。你应该:
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(new TreeNode(node));
}
关于添加子子节点:
foreach (var node in subNodes)
{
var treeNode = new TreeNode(node);
//Call function that returns all the sub-sub nodes
//Assign those nodes to 'treeNode' using another foreach - or better still have this as a recursive function
parentNode1.ChildNodes.Add(treeNode);
}
你应该使用 TreeNode 类型的字符串试试这个,
foreach (var node in subNodes)
{
parentNode1.ChildNodes.Add(new TreeNode(node));
}