如何填充与被保存在数据库中的文件路径树状 [英] How to populate treeview with file path which is saved in database

查看:142
本文介绍了如何填充与被保存在数据库中的文件路径树状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有数据库中保存的文件路径。现在我想通过展示这些树状文件路径。我发现一个样本的工作正常,但不知道什么时候会有巨大的数据库数据,那么树形视图人口将被挂起或花费太长时间。在这里我给的代码。请检查并告诉它可以纠正,结果表现将好时,会有分贝巨大的数据。

suppose i have saved file path in database. now i want to show those file path through treeview. i found one sample that works fine but do not know when there will be huge data in database then treeview population will be hang or take too long. here i am giving the code. please check and tell which can be rectified as a result performance will be good when there will be huge data in db.

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();
    }



感谢

thanks

推荐答案

根据您的框架版本,也许你可以尝试这样的事:

Depending on your framework version, maybe you can try something like this :

public void ProcessPath(IEnumerable<String> path,  TreeNodeCollection nodes)
{
    if (!path.Any())
        return;
    var node = nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == path.First());
    if (node == null)
    {
        node = new TreeNode(text: path.First());
        nodes.Add(node);
    }
    ProcessPath(path.Skip(1),node.ChildNodes);
}

public void CreateTreeView()
{
    foreach (string field in MyDataBase.FieldsInMyColumn())
        ProcessPath(field.Split('\\'),myTreeView.Nodes);
}

如果你真的有一个巨大的行数量,你应该寻找一个解决方案,其中只一个节点

If you really have a huge amount of rows, you should probably look for a solution where you only load the child nodes upon click on a node

这篇关于如何填充与被保存在数据库中的文件路径树状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆