0

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

Thomas
  • 31,089
  • 118
  • 335
  • 589
  • for my own reference. http://stackoverflow.com/questions/6415037/populate-treeview-from-list-of-file-paths-in-wpf http://stackoverflow.com/questions/673931/file-system-treeview http://stackoverflow.com/questions/2436201/how-to-make-a-treeview-from-a-database-column-filepath – Thomas Nov 06 '12 at 14:35

2 Answers2

1

The first that I could suggest to improve is to use while instead of foreach:

instead of this in both places:

bool exits = false;
foreach (TreeNode node in myTreeview.Nodes)
{
        if (node.Text == elements[i])
        {
                exits = true;
                parentNode = node;
        }
}

you can use

bool exits = false;
int j = 0;
while (!exits && j<myTreeview.Nodes.Count)
{
        if (myTreeview.Nodes[j].Text == elements[i])
        {
               exits = true;
               parentNode = node;
        }
        j++;
}

This way you won't need to iterate through the whole Nodes collection and the loop will finish just after it finds the parent node. Off course, you will go through the whole collection in the cases there isn't Node titled elements[i].

P.S. I think you meant exists instead of exits

Nikola Davidovic
  • 8,189
  • 1
  • 25
  • 32
1

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

jbl
  • 13,949
  • 3
  • 29
  • 90