[ACCEPTED]-How do you databind to a System.Windows.Forms.Treeview control?-treeview

Accepted answer
Score: 10

You are correct in that there is no data 9 binding. The reason being is that TreeViews 8 are hierarchical data structures. That 7 is, not a straight list. As a result the 6 databind option is invalid to say a List 5 structure.

Sadly it's creating your own populate 4 methods or buying 3rd party controls (which 3 in the end will have their own populate 2 methods.)

Here's a decent MSDN article on 1 Binding Hierarchical Data.

Score: 5

I use the tree control from Developer's 7 Express. It will take a table of data and 6 display/edit it in a hierarchical fashion.
All 5 it needs is a primary key field and a parent 4 id field in the table and it can figure 3 out what goes where.

You can do the same 2 thing if you roll your own code and use 1 your own class.

class Node
{
    System.Collections.Generic.List<Node> _Children;
    String Description;

    void Node()
    {
      _Children = new System.Collections.Generic.List<Node>();
    }

    public System.Collections.Generic.List<Node> Children()
    {
      return (_Children);
    }
}

class Program
{
    static void Main(string[] args)
    {
      System.Collections.Generic.List<Node> myTree = new System.Collections.Generic.List<Node>();
      Node firstNode = new Node();
      Node childNode = new Node();
      firstNode.Children().Add(childNode);
    }
}
Score: 2

If it's only a couple levels, I like to 4 populate a dataset with a couple tables 3 and set up a DataRelation on the columns. Then 2 you use some nested loops and create your 1 tree nodes.

More Related questions