如何从数据库列文件路径创建树状视图 [英] How to make a treeview from a database column filepath

查看:54
本文介绍了如何从数据库列文件路径创建树状视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库表中有一列,其中包含表中每个文件的文件路径.我该如何在c#中创建一个树状视图,以模仿数据库中的文件路径列.

I have a column in a database table which contains the filepath for each file in the table. How can I make a treeview in c# which will mimic the filepath column in my database.

这是示例文件路径列在该列中的样子:

Here is what a sample filepath column looks like in the column:

jsmith/project1/hello.cs
jsmith/project1/what.cs
jwilliams/project2/hello.cs

推荐答案

我举了一个小例子.我已经对其进行了测试,并且效果很好.

I've made a little example. I've test it and it works fine.

请注意,我制作了一个MyDataBase类来模拟您的数据库:

Note that I've made a class MyDataBase to simulate your database:

public void CreateTreeView()
{
    TreeView myTreeview = new TreeView();
    myTreeview.Dock = DockStyle.Fill;
    this.Controls.Add(myTreeview);

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

编辑

在这里,我粘贴了我不需要的辅助代码,但这将帮助您理解我的代码,或者自己复制/粘贴并尝试一下.

Here I paste my auxiliar code, that you don´t need, but it will help you to understand my code, or to copy/paste and try it by yourself.

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

form1中的构造函数

public Form1()
{

    InitializeComponent();

    MyDataBase.AddField("jsmith/project1/hello.cs");
    MyDataBase.AddField("jsmith/project1/what.cs");
    MyDataBase.AddField("jsmith/project2/hello.cs");

    CreateTreeView();
}

这篇关于如何从数据库列文件路径创建树状视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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