[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bulid a treeview dynamicly from a list array

Posted on 2007-07-30
9
Medium Priority
?
269 Views
Last Modified: 2010-04-15
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; }
      }
    }
0
Comment
Question by:kwickway
  • 6
  • 3
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19598954
Are you talking about ASP.NET or WinForms?  2005 or 2003?

Bob
0
 

Author Comment

by:kwickway
ID: 19599567
Winforms.  Basicly i want to populate a treeview with virtual folders out of the data above.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19601653
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:kwickway
ID: 19601921
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
 

Author Comment

by:kwickway
ID: 19602374
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
 

Author Comment

by:kwickway
ID: 19602541
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1640 total points
ID: 19602597
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
 

Author Comment

by:kwickway
ID: 19602722
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
 

Author Comment

by:kwickway
ID: 19603215
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question