How do you databind to a System.Windows.Forms.Treeview control?

后端 未结 3 1888
[愿得一人]
[愿得一人] 2020-12-01 18:37

I\'m looking at this control, and it seems to be lacking the standard .net \"datasource\" and \"datamember\" properties for databinding. Is this control not bindable? I ca

相关标签:
3条回答
  • 2020-12-01 19:02

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

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

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

    0 讨论(0)
  • 2020-12-01 19:07

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

    0 讨论(0)
  • 2020-12-01 19:25

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

    You can do the same thing if you roll your own code and use 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题