1

I don't know how to show show all data in a treeview control: here is my code:

 private void PopulateTree(string path, int depth, TreeNode parent)
    {
        if (depth == 0)
        {
        //This make a child
            parent.Nodes.Add(new TreeNode(path);
            return;
        }
        //This makes a parent
        TreeNode first = new TreeNode(path);
        parent.Nodes.Add(first);

        foreach (var v in ListWithPaths)
        {
            PopulateTree(v, depth - 1, first);
        }
    }

It only seems to works when depth=1

parent
-parent
--parent
---child
---child
--parent
---child
---child
-/parent
/parent

This is how I see it....

Michael Petrotta
  • 56,954
  • 26
  • 136
  • 173
Jake
  • 97
  • 2
  • 4
  • 13
  • It only makes one because you are continually making parent nodes and only make a child when you hit zero. Are you using depth to indicate you want a child node? – Kixoka Jun 27 '13 at 17:26
  • I need something like this I updated my question – Jake Jun 27 '13 at 17:31
  • Try refactoring your code to have 2 methods one to add a parent and one to add a child.... you may need to decide ahead of time what constitutes a parent and create your collection accordingly and then just spin through the collection and populate accordingly. Honestly there are a lot of examples if you google it... – Kixoka Jun 27 '13 at 17:40
  • please could you provide a link, cause I googleit but maybe I'm looking in wrong direction.... – Jake Jun 27 '13 at 17:41
  • @user2508298, you have a habit of removing the body of your questions after they get answers. This is disrespectful to the users who devote time and effort to answering your questions. Please stop. – Michael Petrotta Jul 24 '13 at 00:01

2 Answers2

0

I have found this answer from here :

private void Form1_Load(object sender, EventArgs e)
    {
        var paths = new List<string>
                        {
                            @"C:\WINDOWS\AppPatch\MUI\040C",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
                            @"C:\WINDOWS\addins",
                            @"C:\WINDOWS\AppPatch",
                            @"C:\WINDOWS\AppPatch\MUI",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
                        };

        treeView1.PathSeparator = @"\";

        PopulateTreeView(treeView1, paths, '\\');
}


private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
    {
        TreeNode lastNode = null;
        string subPathAgg;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(pathSeparator))
            {
                subPathAgg += subPath + pathSeparator;
                TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                if (nodes.Length == 0)
                    if (lastNode == null)
                        lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                else
                    lastNode = nodes[0];
            }
        }
    }

        foreach (var v in ListWithPaths)
        {
            PopulateTree(v, depth - 1, first);
        }
    }

Based on the path, it's creating the depth. If this is not the wished behaviour, you can change the PopulateTreeView-Function accordingly.

Community
  • 1
  • 1
Fabian Bigler
  • 8,858
  • 4
  • 35
  • 61
0

There are some unclear moments in your code:

  1. Do you want to explicitly restrict path depth (e.g. show paths up to 3d level only)
  2. What is ListWithPaths? Can two paths have a common node?

To show a single path with restriction (depth) the code could be

private void PopulateTree(String path, TreeNode parent, int depth) {
  if (depth == 0) // <- Artificial criterium
    return;

  if (String.IsNullOrEmpty(path))
    return;

  int index = path.IndexOf(Path.DirectorySeparatorChar);

  String directoryName = (index < 0) ? path : path.Substring(0, index);
  String otherName = (index < 0) ? null : path.Substring(index + 1);

  TreeNode childNode = parent.Nodes.Add(directoryName);

  PopulateTree(otherName, childNode, depth - 1);
}

In order to load a collection of paths without any restriction with possible common nodes you can use something like this:

private void PopulateTree(String path, TreeView view, TreeNode parent) {
  if (String.IsNullOrEmpty(path))
    return;

  int index = path.IndexOf(Path.DirectorySeparatorChar);

  String directoryName = (index < 0) ? path : path.Substring(0, index);
  String otherName = (index < 0) ? null : path.Substring(index + 1);

  TreeNode childNode = null;
  TreeNodeCollection nodes = (parent == null) ? view.Nodes : parent.Nodes;

  foreach (TreeNode node in nodes)
    if (String.Equals(node.Name, directoryName)) {
      childNode = node;

      break;
    }

  if (childNode == null)
    childNode = nodes.Add(directoryName);

  PopulateTree(otherName, view, childNode);
}

private void PopulateTree(IEnumerable<String> paths, TreeView view) {
  view.BeginUpdate();

  try {
    foreach (String path in paths)
      PopulateTree(path, view, null);
  }
  finally {
    view.EndUpdate();
  }
}

...

PopulateTree(ListWithPaths, MyTreeView)
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186