我尝试在运行时分层次填充TreeView,但在我的代码中发生了错误。
通常我应该有很多项目作为根节点,然后是子项目作为子节点,依此类推。
让我们说项目1,9,10,62和65是根或项目。
问题:代码不断向对方添加根节点。因此它将下一个根节点视为前一个节点的子节点。
结果:代码应该创建具有子项目的分离根节点(子项目也可以包含子项目。)
代码:
List<string> lst
更新
<Input type="Checkbox" id="autoPlay">Autoplay</Input>
<script type="text/JavaScript>//in the body
var checkBox = document.getElementById("autoPlay");
if(checkBox.checked == true){
setInterval(gallery("vor"), 3000);
}
</script>
| 1 | | 9 | | 10 | | 62 | | 65 | | 67 | | 78 | | 83 | | 86 | | 105 | | 116 | | 125 | | 10 | 2 | | 67 | 4 | | 1 | 17 | | 1 | 24 | | 1 | 33 | | 1 | 34 | | 1 | 35 | | 1 | 61 | | 62 | 63 | | 62 | 64 | | 67 | 68 | | 65 | 69 | | 65 | 70 | | 65 | 71 | | 65 | 72 | | 65 | 75 |
答案 0 :(得分:1)
在进入内循环之前,您应该将lastNode
变量重置为null
。为避免混淆和类似错误,最好在需要的地方声明和初始化变量:
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
foreach (string path in paths)
{
string subPathAgg = string.Empty;
TreeNode lastNode = null;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}