TreeView 层次结构生成错误
The TreeView hierarchy is generated wrongly
我尝试在运行时按层次结构填充 TreeView,但我的代码中出现错误。
一般我应该有很多项目作为根节点,然后是子项目作为子节点等等。
假设项目 1、9、10、62 和 65 是根或项目。
- 项目 #1 包含子项目 17、24、33、34、35、61、98...
- 项目 #9 不包含子项目。
- 项目 #10 包含子项目 2 和 104。
- 项目 #62 包含子项目 63、64、108、109。
- 项目 #65 又有其他子项目。
问题: 代码不停地互相添加根节点。所以它将下一个根节点视为前一个根节点的子节点。
结果:代码应该创建带有子项目的独立根节点(子项目也可以包含子项目)。
代码:
private void button2_Click(object sender, EventArgs e)
{
DbConnector db = new DbConnector();
string str = "";
List<string> lst = db.ReadProjectsTable();
lst.OrderBy(x => x.Count(y => y == '|'));
List<string> newLst = new List<string>();
foreach (var item in lst)
{
string output = "";
foreach (var item2 in item.Split('|', '|'))
{
output += item2 + '-';
}
output = output.Substring(output.IndexOf('-')+1, output.LastIndexOf('-')-2);
newLst.Add(output);
str += output + Environment.NewLine;
}
textBox2.Text = str;
tvProjects.PathSeparator = @"-";
PopulateTreeView(tvProjects, newLst, '-');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
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];
}
}
}
更新:
List<string> lst
的样本数据
|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|
在进入内部循环之前,您应该将 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];
}
}
}
我尝试在运行时按层次结构填充 TreeView,但我的代码中出现错误。
一般我应该有很多项目作为根节点,然后是子项目作为子节点等等。
假设项目 1、9、10、62 和 65 是根或项目。
- 项目 #1 包含子项目 17、24、33、34、35、61、98...
- 项目 #9 不包含子项目。
- 项目 #10 包含子项目 2 和 104。
- 项目 #62 包含子项目 63、64、108、109。
- 项目 #65 又有其他子项目。
问题: 代码不停地互相添加根节点。所以它将下一个根节点视为前一个根节点的子节点。
结果:代码应该创建带有子项目的独立根节点(子项目也可以包含子项目)。
代码:
private void button2_Click(object sender, EventArgs e)
{
DbConnector db = new DbConnector();
string str = "";
List<string> lst = db.ReadProjectsTable();
lst.OrderBy(x => x.Count(y => y == '|'));
List<string> newLst = new List<string>();
foreach (var item in lst)
{
string output = "";
foreach (var item2 in item.Split('|', '|'))
{
output += item2 + '-';
}
output = output.Substring(output.IndexOf('-')+1, output.LastIndexOf('-')-2);
newLst.Add(output);
str += output + Environment.NewLine;
}
textBox2.Text = str;
tvProjects.PathSeparator = @"-";
PopulateTreeView(tvProjects, newLst, '-');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
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];
}
}
}
更新:
List<string> lst
|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|
在进入内部循环之前,您应该将 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];
}
}
}