我有一个以下的数据列表,我想在树中像时尚一样显示。 我能够显示两个级别的子文件夹(顺序)。但这个问题是 它可以有任意数量的子文件夹。看起来像递归是要走的路 但不知道如何继续。有什么帮助吗?
ItemName ItemID NodeID ParentNodeID NodeName
0 100 null Node1
Item1 123 100 null
Item2 124 100 null
0 101 null Node2
Item3 125 101 null
Item4 126 101 null
0 103 101 Node3
Item5 127 103 null
0 104 103 Node4
Item6 128 104 null
0 105 null Node5
Item5 127 105 null
Node1
------Item1
------Item2
Node2
------Item3
------Item4
------Node3
--------Item5
--------Node4
------Item6
Node5
-------Item5
Loop through my data
If parentnodeid is null
find in the list object whether folder exists
if so append an item to it and add to the list
else create folder and add to the list
else
process sub folder
if itemid = 0
find in the list whether the folder exist
if so, append to the parent folder and add to the list
else
add the item to the list
end if
答案 0 :(得分:0)
我有一些建议来实现你的目标:
- 递归是真正的方式
- 让您的数据更简单
- 以相同的方式处理项目和节点
示例数据
NodeName NodeID ParentNodeID
Node1 1 0
Node2 2 0
Node3 3 2
Node4 4 3
Node5 5 0
Item1 6 1
Item2 7 1
Item3 8 2
Item4 9 2
Item5 10 3
Item6 11 4
Item5 12 5
下载代码将其转换为树视图
SampleDataTable = new DataTable();
SampleDataTable.Columns.Add("NodeName", typeof(string));
SampleDataTable.Columns.Add("NodeID", typeof(int));
SampleDataTable.Columns.Add("ParentNodeID", typeof(int));
//Insert sample data to this table
SampleDataTable.Rows.Add("Node1", 1, 0);
SampleDataTable.Rows.Add("Node2", 2, 0);
SampleDataTable.Rows.Add("Node3", 3, 2);
SampleDataTable.Rows.Add("Node4", 4, 3);
SampleDataTable.Rows.Add("Node5", 5, 0);
SampleDataTable.Rows.Add("Item1", 6, 1);
SampleDataTable.Rows.Add("Item2", 7, 1);
SampleDataTable.Rows.Add("Item3", 8, 2);
SampleDataTable.Rows.Add("Item4", 9, 2);
SampleDataTable.Rows.Add("Item5", 10, 3);
SampleDataTable.Rows.Add("Item6", 11, 4);
SampleDataTable.Rows.Add("Item7", 12, 5);
treeView1.Nodes.Clear();
foreach (DataRow dr in SampleDataTable.Rows)
{
if ((int)dr["ParentNodeID"] == 0)
{
TreeNode parentNode = new TreeNode();
parentNode .Text = dr["NodeName"].ToString();
string value = dr["NodeID"].ToString();
treeView1.Nodes.Add(parentNode);
findNode(parentNode, value);
}
}
public void findNode(TreeNode parent, string Id)
{
IEnumerable<DataRow> rows = SampleDataTable.Select("ParentNodeID=" + Id);
foreach (DataRow dr in rows)
{
//Create child node
TreeNode child = new TreeNode();
child.Text = dr["NodeName"].ToString().Trim();
parent.Nodes.Add(child);
//Add more child nodes recursively
findNode(child, dr["NodeID"].ToString());
}
}
答案 1 :(得分:0)
如果您想将其打印为文本,我可以通过此帖https://stackoverflow.com/a/1649223/2966790
获取此方法稍微调整一下
SampleDataTable = new DataTable();
SampleDataTable.Columns.Add("NodeName", typeof(string));
SampleDataTable.Columns.Add("NodeID", typeof(int));
SampleDataTable.Columns.Add("ParentNodeID", typeof(int));
//Insert sample data to this table
SampleDataTable.Rows.Add("Node1", 1, 0);
SampleDataTable.Rows.Add("Node2", 2, 0);
SampleDataTable.Rows.Add("Node3", 3, 2);
SampleDataTable.Rows.Add("Node4", 4, 3);
SampleDataTable.Rows.Add("Node5", 5, 0);
SampleDataTable.Rows.Add("Item1", 6, 1);
SampleDataTable.Rows.Add("Item2", 7, 1);
SampleDataTable.Rows.Add("Item3", 8, 2);
SampleDataTable.Rows.Add("Item4", 9, 2);
SampleDataTable.Rows.Add("Item5", 10, 3);
SampleDataTable.Rows.Add("Item6", 11, 4);
SampleDataTable.Rows.Add("Item7", 12, 5);
Node tree = new Node();
foreach (DataRow dr in SampleDataTable.Rows)
{
if ((int)dr["ParentNodeID"] == 0)
{
Node parentNode = new Node();
parentNode.Name = dr["NodeName"].ToString();
string value = dr["NodeID"].ToString();
tree.Children.Add(parentNode);
findNode(parentNode, value);
}
}
PrintPretty(tree, " ", true);
用于填充树并打印它的函数:
public void findNode(Node parent, string Id)
{
IEnumerable<DataRow> rows = SampleDataTable.Select("ParentNodeID=" + Id);
foreach (DataRow dr in rows)
{
//Create child node
Node child = new Node();
child.Name = dr["NodeName"].ToString().Trim();
parent.Children.Add(child);
//Add more child nodes recursively
findNode(child, dr["NodeID"].ToString());
}
}
public void PrintPretty(Node node, string indent, bool last)
{
textBox1.AppendText(indent);
if (last)
{
textBox1.AppendText("\\-");
indent += " ";
}
else
{
textBox1.AppendText("|-");
indent += "| ";
}
textBox1.AppendText(node.Name + Environment.NewLine);
List<Node> Children = node.Children;
for (int i = 0; i < Children.Count; i++)
PrintPretty(Children[i], indent, i == Children.Count - 1);
}
节点类:
public class Node
{
public string Name; // method name
public List<Node> Children;
}
结果:
答案 2 :(得分:-1)
伪请以此为指导:
// setup and call function:
string indent = "";
var toplevel = nodelist.Where(x => x.itemid == 0 && x.parentid == null).ToList();
displaytree(toplevel,indent);
// recursive function
displaytree(List<node> display,string indent)
{
foreach(item in display)
{
Console.WriteLine(indent+item.NodeName);
foreach(inneritem in nodelist.Where(x => item.nodeid == x.nodeid && x.itemid != null)
{
Console.WriteLine(indent+"------"+inneritem.NodeName);
}
displaytree(nodelist.Where(x => x.parentid == item.nodeid),indent+" ");
}
}