假设我已经在数据库中保存了文件路径。现在我想通过树视图显示这些文件路径。我发现一个样本工作正常,但不知道数据库中何时会有大量数据,然后树视图人口将挂起或花费太长时间。我在这里给出代码。请检查并告诉哪些可以纠正,因为当数据库中有大量数据时性能会很好。
public static class MyDataBase
{
private static List<string> fields = new List<string>();
public static void AddField(string field)
{
fields.Add(field);
}
public static IList<string> FieldsInMyColumn()
{
return fields;
}
}
public void CreateTreeView()
{
foreach (string field in MyDataBase.FieldsInMyColumn())
{
string[] elements = field.Split('\\');
TreeNode parentNode = null;
for (int i = 0; i < elements.Length - 1; ++i)
{
if (parentNode == null)
{
bool exits = false;
foreach (TreeNode node in myTreeview.Nodes)
{
if (node.Text == elements[i])
{
exits = true;
parentNode = node;
}
}
if (!exits)
{
TreeNode childNode = new TreeNode(elements[i]);
myTreeview.Nodes.Add(childNode);
parentNode = childNode;
}
}
else
{
bool exits = false;
foreach (TreeNode node in parentNode.Nodes)
{
if (node.Text == elements[i])
{
exits = true;
parentNode = node;
}
}
if (!exits)
{
TreeNode childNode = new TreeNode(elements[i]);
parentNode.Nodes.Add(childNode);
parentNode = childNode;
}
}
}
if (parentNode != null)
{
parentNode.Nodes.Add(elements[elements.Length - 1]);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
MyDataBase.AddField(@"c:\jsmith\project1\hello.cs");
MyDataBase.AddField(@"c:\jsmith\project1\what.cs");
MyDataBase.AddField(@"c:\jsmith\project2\hello.cs");
MyDataBase.AddField(@"c:\jsmith\project1\tdp.cs");
MyDataBase.AddField(@"c:\jsmith\project2\ship.cs");
MyDataBase.AddField(@"d:\jsmith\project1\hello404.cs");
MyDataBase.AddField(@"c:\jsmith1\project2\ship.cs");
CreateTreeView();
}
谢谢