Bulid a treeview dynamicly from a list array

I have this List<T> and i am trying to figure out how to populate it to a treeview in the correct order

      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "a folder"));
      folders.Add(new Folder(5, 2, "another folder"));
      folders.Add(new Folder(6, 4, "a"));
      folders.Add(new Folder(15, 4, "b"));
      folders.Add(new Folder(16, 6, "c"));
      folders.Add(new Folder(17, 3, "d"));
      folders.Add(new Folder(18, 5, "e"));
      folders.Add(new Folder(19, 7, "f"));
      folders.Add(new Folder(7, 5, "g"));
      folders.Add(new Folder(8, 6, "New"));
      folders.Add(new Folder(13, 2, "h"));
      folders.Add(new Folder(14, 2, "i"));
      folders.Add(new Folder(1, 0, "root"));
      folders.Add(new Folder(2, 1, "j"));
      folders.Add(new Folder(3, 2, "k"));
      folders.Add(new Folder(9, 5, "l"));
      folders.Add(new Folder(10, 4, "m"));
      folders.Add(new Folder(11, 3, "n"));
      folders.Add(new Folder(12, 4, "o"));

I want the treeview to be built dynamicly with the content above

The first pram is the Primary KEY (unique) from the database
The second pram is what parent it should go under as a child
and the third pram is just a name of the folder



Below is my object




    public class Folder
    {
      public Folder()
      {
      }

      public Folder(Int32 ID, Int32 Parent, String FolderName)
      {
        _ID = ID;
        _Parent = Parent;
        _FolderName = FolderName;
      }

      private Int32 _ID = 0;

      public Int32 ID
      {
        get { return _ID; }
        set { _ID = value; }
      }
      private Int32 _Parent = 0;

      public Int32 Parent
      {
        get { return _Parent; }
        set { _Parent = value; }
      }
      private String _FolderName = String.Empty;

      public String FolderName
      {
        get { return _FolderName; }
        set { _FolderName = value; }
      }
    }
kwickwayAsked:
Who is Participating?
 
Bob LearnedCommented:
Try this:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

public partial class formTree : Form
{
    public formTree()
    {
        InitializeComponent();
    }

    private void formTree_Load(object sender, EventArgs e)
    {

        List<Folder> folders = new List<Folder>();
        folders.Add(new Folder(1, 0, "root"));
        folders.Add(new Folder(2, 1, "j"));
        folders.Add(new Folder(3, 2, "k"));
        folders.Add(new Folder(4, 2, "a folder"));
        folders.Add(new Folder(5, 2, "another folder"));
        folders.Add(new Folder(6, 4, "a"));
        folders.Add(new Folder(7, 5, "g"));
        folders.Add(new Folder(8, 6, "New"));
        folders.Add(new Folder(9, 5, "l"));
        folders.Add(new Folder(10, 4, "m"));
        folders.Add(new Folder(11, 3, "n"));
        folders.Add(new Folder(12, 4, "o"));
        folders.Add(new Folder(13, 2, "h"));
        folders.Add(new Folder(14, 2, "i"));
        folders.Add(new Folder(15, 4, "b"));
        folders.Add(new Folder(16, 6, "c"));
        folders.Add(new Folder(17, 3, "d"));
        folders.Add(new Folder(18, 5, "e"));
        folders.Add(new Folder(19, 7, "f"));


        foreach (Folder folder in folders)
        {
            TreeNode node = new TreeNode(folder.FolderName);
            node.Name = folder.ID.ToString();

            TreeNode[] find = this.treeView1.Nodes.Find(folder.Parent.ToString(), false);

            if (find == null || find.Length == 0)
                treeView1.Nodes.Add(node);
            else
            {
                find[0].Nodes.Add(node);
            }

        }

    }

}

Bob
0
 
Bob LearnedCommented:
Are you talking about ASP.NET or WinForms?  2005 or 2003?

Bob
0
 
kwickwayAuthor Commented:
Winforms.  Basicly i want to populate a treeview with virtual folders out of the data above.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Bob LearnedCommented:
I would suggest something like this:

1) Build a tree structure, collecting all child paths for a path

            Dictionary<int, List<string>> tree = new Dictionary<int, List<string>>();

2) Create a TreeNode with all of its children:

            foreach (DataRow dr in dt.Rows)
            {
                int id = Convert.ToInt32(dr["ID"]);
                int parent = Convert.ToInt32(dr["Parent"]);
                string name = dr["Name"].ToString();

                if (!tree.ContainsKey(parent))
                {
                    tree.Add(id, new List<Folder>());
                }
                tree[parent].Add(folder);
            }

3) Then, build the tree from that structure with this form of the TreeNode constructor:

    TreeNode node = new TreeNode(text, TreeNode[]);

Bob
0
 
kwickwayAuthor Commented:
Hello, i could not figure it out....

Here is my code in my entire form... I have just a treeView1 control docked to full in the form1 and thats all i have...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "a folder"));
      folders.Add(new Folder(5, 2, "another folder"));
      folders.Add(new Folder(6, 4, "a"));
      folders.Add(new Folder(15, 4, "b"));
      folders.Add(new Folder(16, 6, "c"));
      folders.Add(new Folder(17, 3, "d"));
      folders.Add(new Folder(18, 5, "e"));
      folders.Add(new Folder(19, 7, "f"));
      folders.Add(new Folder(7, 5, "g"));
      folders.Add(new Folder(8, 6, "New"));
      folders.Add(new Folder(13, 2, "h"));
      folders.Add(new Folder(14, 2, "i"));
      folders.Add(new Folder(1, 0, "root"));
      folders.Add(new Folder(2, 1, "j"));
      folders.Add(new Folder(3, 2, "k"));
      folders.Add(new Folder(9, 5, "l"));
      folders.Add(new Folder(10, 4, "m"));
      folders.Add(new Folder(11, 3, "n"));
      folders.Add(new Folder(12, 4, "o"));

      Dictionary<int, List<string>> tree = new Dictionary<int, List<string>>();
      foreach (DataRow dr in dt.Rows)
      {
        int id = Convert.ToInt32(dr["ID"]);
        int parent = Convert.ToInt32(dr["Parent"]);
        string name = dr["Name"].ToString();

        if (!tree.ContainsKey(parent))
        {
          tree.Add(id, new List<Folder>());
        }
        tree[parent].Add(folder);
      }
      TreeNode node = new TreeNode(text, TreeNode[]);

      treeView1.Nodes.Add(node);

    }

    public class Folder
    {
      public Folder()
      {
      }

      public Folder(Int32 ID, Int32 Parent, String FolderName)
      {
        _ID = ID;
        _Parent = Parent;
        _FolderName = FolderName;
      }

      private Int32 _ID = 0;

      public Int32 ID
      {
        get { return _ID; }
        set { _ID = value; }
      }
      private Int32 _Parent = 0;

      public Int32 Parent
      {
        get { return _Parent; }
        set { _Parent = value; }
      }
      private String _FolderName = String.Empty;

      public String FolderName
      {
        get { return _FolderName; }
        set { _FolderName = value; }
      }
    }
  }
}


I played with it a little bit but i couldn't get anything out of it, so i put everything back to normal like you had it.  Can you help???
0
 
kwickwayAuthor Commented:
Here is what i tried....


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "a folder"));
      folders.Add(new Folder(5, 2, "another folder"));
      folders.Add(new Folder(6, 4, "a"));
      folders.Add(new Folder(15, 4, "b"));
      folders.Add(new Folder(16, 6, "c"));
      folders.Add(new Folder(17, 3, "d"));
      folders.Add(new Folder(18, 5, "e"));
      folders.Add(new Folder(19, 7, "f"));
      folders.Add(new Folder(7, 5, "g"));
      folders.Add(new Folder(8, 6, "New"));
      folders.Add(new Folder(13, 2, "h"));
      folders.Add(new Folder(14, 2, "i"));
      folders.Add(new Folder(1, 0, "root"));
      folders.Add(new Folder(2, 1, "j"));
      folders.Add(new Folder(3, 2, "k"));
      folders.Add(new Folder(9, 5, "l"));
      folders.Add(new Folder(10, 4, "m"));
      folders.Add(new Folder(11, 3, "n"));
      folders.Add(new Folder(12, 4, "o"));

      Dictionary<int, List<string>> tree = new Dictionary<int, List<string>>();
      foreach (Folder folder in folders)
      {
        if (!tree.ContainsKey(folder.Parent))
        {
          tree.Add(folder.ID,new List<String>());
        }
        tree[folder.Parent].Add(folder.FolderName);
      }
      TreeNode node = new TreeNode("test",TreeNode[]);

      treeView1.Nodes.Add(node);

    }

    public class Folder
    {
      public Folder()
      {
      }

      public Folder(Int32 ID, Int32 Parent, String FolderName)
      {
        _ID = ID;
        _Parent = Parent;
        _FolderName = FolderName;
      }

      private Int32 _ID = 0;

      public Int32 ID
      {
        get { return _ID; }
        set { _ID = value; }
      }
      private Int32 _Parent = 0;

      public Int32 Parent
      {
        get { return _Parent; }
        set { _Parent = value; }
      }
      private String _FolderName = String.Empty;

      public String FolderName
      {
        get { return _FolderName; }
        set { _FolderName = value; }
      }
    }
  }
}


I am not getting this information from a datatable just direct from the list<t>
Let me know if you have any ideas....

Bill.
0
 
kwickwayAuthor Commented:
I modified the code as follows:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "a folder"));
      folders.Add(new Folder(5, 2, "another folder"));
      folders.Add(new Folder(6, 4, "a"));
      folders.Add(new Folder(15, 4, "b"));
      folders.Add(new Folder(16, 6, "c"));
      folders.Add(new Folder(17, 3, "d"));
      folders.Add(new Folder(18, 5, "e"));
      folders.Add(new Folder(19, 7, "f"));
      folders.Add(new Folder(7, 5, "g"));
      folders.Add(new Folder(8, 6, "New"));
      folders.Add(new Folder(13, 2, "h"));
      folders.Add(new Folder(14, 2, "i"));
      folders.Add(new Folder(1, 0, "root"));
      folders.Add(new Folder(2, 1, "j"));
      folders.Add(new Folder(3, 2, "k"));
      folders.Add(new Folder(9, 5, "l"));
      folders.Add(new Folder(10, 4, "m"));
      folders.Add(new Folder(11, 3, "n"));
      folders.Add(new Folder(12, 4, "o"));

      // Sorts the list by ID
      folders.Sort(delegate(Folder x, Folder y)
      {
        return String.Compare(x.ID.ToString("0000000"), y.ID.ToString("0000000"));
      });

      TreeNode tree = new TreeNode();

      foreach (Folder folder in folders)
      {
        if (!tree.Nodes.ContainsKey(folder.Parent.ToString()))
        {
          tree.Nodes.Add(folder.ID.ToString(), folder.FolderName);
        }
        tree.Nodes[folder.Parent.ToString()].Nodes.Add(folder.ID.ToString(), folder.FolderName);
      }
      treeView1.Nodes.Add(tree);

    }
  }
 
  public class Folder
    {
      public Folder()
      {
      }

      public Folder(Int32 ID, Int32 Parent, String FolderName)
      {
        _ID = ID;
        _Parent = Parent;
        _FolderName = FolderName;
      }

      private Int32 _ID = 0;

      public Int32 ID
      {
        get { return _ID; }
        set { _ID = value; }
      }
      private Int32 _Parent = 0;

      public Int32 Parent
      {
        get { return _Parent; }
        set { _Parent = value; }
      }
      private String _FolderName = String.Empty;

      public String FolderName
      {
        get { return _FolderName; }
        set { _FolderName = value; }
      }
    }
}


It works almost.   It works when there are no sub-children involved, i only displays half of the list, but this is only when i rememve line 55:
      tree.Nodes[folder.Parent.ToString()].Nodes.Add(folder.ID.ToString(), folder.FolderName);
However when i put it back and put a break point anywhere in the forloop it only goes thorugh it once, and this line (above) will actually terminate or break the foreach loop...
0
 
kwickwayAuthor Commented:
HEY BOB!!!  Thanks so much!  It worked after i turned to boolean value to search all nodes to true.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "a folder"));
      folders.Add(new Folder(5, 2, "another folder"));
      folders.Add(new Folder(6, 4, "a"));
      folders.Add(new Folder(15, 4, "b"));
      folders.Add(new Folder(16, 6, "c"));
      folders.Add(new Folder(17, 3, "d"));
      folders.Add(new Folder(18, 5, "e"));
      folders.Add(new Folder(19, 7, "f"));
      folders.Add(new Folder(7, 5, "g"));
      folders.Add(new Folder(8, 6, "New"));
      folders.Add(new Folder(13, 2, "h"));
      folders.Add(new Folder(14, 2, "i"));
      folders.Add(new Folder(1, 0, "root"));
      folders.Add(new Folder(2, 1, "j"));
      folders.Add(new Folder(3, 2, "k"));
      folders.Add(new Folder(9, 5, "l"));
      folders.Add(new Folder(10, 4, "m"));
      folders.Add(new Folder(11, 3, "n"));
      folders.Add(new Folder(12, 4, "o"));

      // Sorts the list by ID
      folders.Sort(delegate(Folder x, Folder y)
      {
        return String.Compare(x.ID.ToString("0000000"), y.ID.ToString("0000000"));
      });

      foreach (Folder folder in folders)
      {
        TreeNode node = new TreeNode(folder.FolderName);
        node.Name = folder.ID.ToString();

        TreeNode[] find = this.treeView1.Nodes.Find(folder.Parent.ToString(),true);

        if (find == null || find.Length == 0)
          treeView1.Nodes.Add(node);
        else
        {
          find[0].Nodes.Add(node);
        }
      }
    }
  }
 
  public class Folder
    {
      public Folder()
      {
      }

      public Folder(Int32 ID, Int32 Parent, String FolderName)
      {
        _ID = ID;
        _Parent = Parent;
        _FolderName = FolderName;
      }

      private Int32 _ID = 0;

      public Int32 ID
      {
        get { return _ID; }
        set { _ID = value; }
      }
      private Int32 _Parent = 0;

      public Int32 Parent
      {
        get { return _Parent; }
        set { _Parent = value; }
      }
      private String _FolderName = String.Empty;

      public String FolderName
      {
        get { return _FolderName; }
        set { _FolderName = value; }
      }
    }
}



Thanks again so much, this was exactly what i wanted.
Bill.
0
 
kwickwayAuthor Commented:
Also to make it look neater i chnaged a few things....


I hope this helps someone else too in the future...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // Populates a sample tree of virtual folders
      List<Folder> folders = new List<Folder>();
      folders.Add(new Folder(4, 2, "Starts with A"));
      folders.Add(new Folder(5, 2, "Starts with C"));
      folders.Add(new Folder(6, 4, "Affenpinscher"));
      folders.Add(new Folder(15, 4, "Afghan Hound "));
      folders.Add(new Folder(16, 6, "Your Dog"));
      folders.Add(new Folder(17, 3, "Beauceron"));
      folders.Add(new Folder(18, 5, "Cardigan Welsh Corgi"));
      folders.Add(new Folder(19, 7, "Your Dog"));
      folders.Add(new Folder(7, 5, "Cairn Terrier "));
      folders.Add(new Folder(8, 6, "My Dog"));
      folders.Add(new Folder(13, 1, "More Information"));
      folders.Add(new Folder(14, 1, "Less Information"));
      folders.Add(new Folder(1, 0, "Dog Types"));
      folders.Add(new Folder(2, 1, "Sorted List"));
      folders.Add(new Folder(3, 2, "Starts with B"));
      folders.Add(new Folder(9, 5, "Canaan Dog"));
      folders.Add(new Folder(10, 4, "Alaskan Malamute"));
      folders.Add(new Folder(11, 3, "Basset Hound"));
      folders.Add(new Folder(12, 4, "American Bulldog "));

      // Sorts the list by ID
      folders.Sort(delegate(Folder x, Folder y)
      {
        return String.Compare(x.ID.ToString("0000000"), y.ID.ToString("0000000"));
      });

      #region "Creates the tree the folders in the tree view"

      foreach (Folder folder in folders)
      {
        TreeNode node = new TreeNode(folder.FolderName);
        node.Name = folder.ID.ToString();

        TreeNode[] find = this.treeView1.Nodes.Find(folder.Parent.ToString(), true);

        if (find == null || find.Length == 0)
          treeView1.Nodes.Add(node);
        else
        {
          find[0].Nodes.Add(node);
        }
      }

      #endregion

      // Sorts the tree view
      treeView1.Sort();
    }
  }

  public class Folder
  {
    public Folder()
    {
    }

    public Folder(Int32 ID, Int32 Parent, String FolderName)
    {
      _ID = ID;
      _Parent = Parent;
      _FolderName = FolderName;
    }

    private Int32 _ID = 0;
    private Int32 _Parent = 0;
    private String _FolderName = String.Empty;

    public Int32 ID
    {
      get { return _ID; }
      set { _ID = value; }
    }

    public Int32 Parent
    {
      get { return _Parent; }
      set { _Parent = value; }
    }

    public String FolderName
    {
      get { return _FolderName; }
      set { _FolderName = value; }
    }
  }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.