TreeView for WinForms | ComponentOne
Data Binding / Bind TreeView to Hierarchical Data
In This Topic
    Bind TreeView to Hierarchical Data
    In This Topic

    An important feature of TreeView is its ability to bind to hierarchical data and display it as relationship between parent and child nodes.

    The C1TreeView class provides the DataSource property to specify the data source for the TreeView control. The class also provides the DataMember property to specify the name of a specific record within the data source.

    Here is a sample code that binds the TreeView control to a hierarchical data source that is created by using different classes.

    The following code snippet creates a class Product.

    ' create a class
    Public Class Product
        Public Property ID() As Guid
            Get
                Return m_ID
            End Get
            Set
                m_ID = Value
            End Set
        End Property
        Private m_ID As Guid
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property Price() As Double
            Get
                Return m_Price
            End Get
            Set
                m_Price = Value
            End Set
        End Property
        Private m_Price As Double
    
        Public Sub New(name__1 As String, price__2 As Double)
            ID = Guid.NewGuid()
            Name = name__1
            Price = price__2
        End Sub
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    
    // create a class
    public class Product
    {
        public Guid ID { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    
        public Product(string name, double price)
        {
            ID = Guid.NewGuid();
            Name = name;
            Price = price;
        }
        public override string ToString()
        {
            return Name;
        }
    }
    

     Here is a code snippet creating a class ProductsGroup.

    ' create a class
    Public Class ProductsGroup
    
        Public Property ID() As Guid
            Get
                Return m_ID
            End Get
            Set
                m_ID = Value
            End Set
        End Property
        Private m_ID As Guid
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property Products() As BindingList(Of Product)
            Get
                Return m_Products
            End Get
            Set
                m_Products = Value
            End Set
        End Property
        Private m_Products As BindingList(Of Product)
        Public ReadOnly Property CountOfProducts() As Integer
            Get
                Return Products.Count
            End Get
        End Property
    
        Public Sub New(name__1 As String)
            ID = Guid.NewGuid()
            Name = name__1
            Products = New BindingList(Of Product)()
        End Sub
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    
    // create a class
    public class ProductsGroup
    {
        public Guid ID { get; set; }
        public string Name { get; set; }
        public BindingList<Product> Products { get; set; }
        public int CountOfProducts
        {
            get { return Products.Count; }
        }
    
        public ProductsGroup(string name)
        {
            ID = Guid.NewGuid();
            Name = name;
            Products = new BindingList<Product>();
        }
    
        public override string ToString()
        {
            return Name;
        }
    }
    

     Below is the code creating a class Store.

    ' create a class
    Public Class Store
    
        Public Property ID() As Guid
            Get
                Return m_ID
            End Get
            Set
                m_ID = Value
            End Set
        End Property
        Private m_ID As Guid
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property ProductsGroups() As BindingList(Of ProductsGroup)
            Get
                Return m_ProductsGroups
            End Get
            Set
                m_ProductsGroups = Value
            End Set
        End Property
        Private m_ProductsGroups As BindingList(Of ProductsGroup)
        Public ReadOnly Property CountOfProducts() As Integer
            Get
                Dim count As Integer = 0
                For Each pg As Object In ProductsGroups
                    count += pg.CountOfProducts
                Next
                Return count
            End Get
        End Property
    
        Public Sub New(name__1 As String)
            ID = Guid.NewGuid()
            Name = name__1
            ProductsGroups = New BindingList(Of ProductsGroup)()
        End Sub
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    
    // create a class
    public class Store
    {
        public Guid ID { get; set; }
        public string Name { get; set; }
        public BindingList<ProductsGroup> ProductsGroups { get; set; }
        public int CountOfProducts
        {
            get
            {
                int count = 0;
                foreach (var pg in ProductsGroups)
                    count += pg.CountOfProducts;
                return count;
            }
        }
    
        public Store(string name)
        {
            ID = Guid.NewGuid();
            Name = name;
            ProductsGroups = new BindingList<ProductsGroup>();
        }
    
        public override string ToString()
        {
            return Name;
        }
    }
    

     The following code snippet creates a class StoreCollection.

    ' create a class
    Public Class StoreCollection
        Inherits BindingList(Of Store)
        Public Shared Function GetData() As StoreCollection
            Dim stores = New StoreCollection()
    
            stores.Add(New Store("Pear Inc."))
            stores(0).ProductsGroups.Add(New ProductsGroup("Mobile phones"))
            stores(0).ProductsGroups(0).Products.Add(New Product("fPhone 34", 999.99))
            stores(0).ProductsGroups(0).Products.Add(New Product("fPhone 34Z", 9999.99))
            stores(0).ProductsGroups(0).Products.Add(New Product("fPhone 34XX", 100000))
            stores(0).ProductsGroups.Add(New ProductsGroup("Notebooks"))
            stores(0).ProductsGroups(1).Products.Add(New Product("DuckBook S", 9999.99))
            stores(0).ProductsGroups(1).Products.Add(New Product("DuckBook Ultra", 14000))
            stores(0).ProductsGroups(1).Products.Add(New Product("DuckBook Pro", 20000))
            stores(0).ProductsGroups.Add(New ProductsGroup("Computers"))
            stores(0).ProductsGroups(2).Products.Add(New Product("DuckPC 3", 10000.99))
            stores(0).ProductsGroups(2).Products.Add(New Product("DuckPro X", 15000))
            stores(0).ProductsGroups(2).Products.Add(New Product("DuckPro Ultra", 19000))
    
            stores.Add(New Store("Space Inc."))
            stores(1).ProductsGroups.Add(New ProductsGroup("Mobile phones"))
            stores(1).ProductsGroups(0).Products.Add(New Product("Rocket 1A", 900))
            stores(1).ProductsGroups(0).Products.Add(New Product("Rocket 2X", 3999))
            stores(1).ProductsGroups(0).Products.Add(New Product("Rocket 3E", 20000))
            stores(1).ProductsGroups.Add(New ProductsGroup("Notebooks"))
            stores(1).ProductsGroups(1).Products.Add(New Product("Shuttle 1A", 9999.99))
            stores(1).ProductsGroups(1).Products.Add(New Product("Shuttle 1X", 14000))
            stores(1).ProductsGroups(1).Products.Add(New Product("Shuttle Pro", 20000))
            stores(1).ProductsGroups.Add(New ProductsGroup("Computers"))
            stores(1).ProductsGroups(2).Products.Add(New Product("IssPC 2D", 10000.99))
            stores(1).ProductsGroups(2).Products.Add(New Product("IssPro 2X", 15000))
            stores(1).ProductsGroups(2).Products.Add(New Product("IssPro Pro", 19000))
    
            stores.Add(New Store("Fruit Inc."))
            stores(2).ProductsGroups.Add(New ProductsGroup("Mobile phones"))
            stores(2).ProductsGroups(0).Products.Add(New Product("Pineapple 1", 2900))
            stores(2).ProductsGroups(0).Products.Add(New Product("Mango 1", 3099))
            stores(2).ProductsGroups(0).Products.Add(New Product("Orange 1", 5000))
            stores(2).ProductsGroups.Add(New ProductsGroup("Notebooks"))
            stores(2).ProductsGroups(1).Products.Add(New Product("Mandarin X", 9999.99))
            stores(2).ProductsGroups(1).Products.Add(New Product("Lemon X", 14000))
            stores(2).ProductsGroups(1).Products.Add(New Product("Lemon Pro", 20000))
            stores(2).ProductsGroups.Add(New ProductsGroup("Computers"))
            stores(2).ProductsGroups(2).Products.Add(New Product("Plum X", 10000.99))
            stores(2).ProductsGroups(2).Products.Add(New Product("Plum Z", 15000))
            stores(2).ProductsGroups(2).Products.Add(New Product("Plum Pro", 19000))
    
            Return stores
        End Function
    End Class
    
    // create a class
    public class StoreCollection : BindingList<Store>
    {
        public static StoreCollection GetData()
        {
            var stores = new StoreCollection();
    
            stores.Add(new Store("Pear Inc."));
            stores[0].ProductsGroups.Add(new ProductsGroup("Mobile phones"));
            stores[0].ProductsGroups[0].Products.Add(new Product("fPhone 34", 999.99));
            stores[0].ProductsGroups[0].Products.Add(new Product("fPhone 34Z", 9999.99));
            stores[0].ProductsGroups[0].Products.Add(new Product("fPhone 34XX", 100000));
            stores[0].ProductsGroups.Add(new ProductsGroup("Notebooks"));
            stores[0].ProductsGroups[1].Products.Add(new Product("DuckBook S", 9999.99));
            stores[0].ProductsGroups[1].Products.Add(new Product("DuckBook Ultra", 14000));
            stores[0].ProductsGroups[1].Products.Add(new Product("DuckBook Pro", 20000));
            stores[0].ProductsGroups.Add(new ProductsGroup("Computers"));
            stores[0].ProductsGroups[2].Products.Add(new Product("DuckPC 3", 10000.99));
            stores[0].ProductsGroups[2].Products.Add(new Product("DuckPro X", 15000));
            stores[0].ProductsGroups[2].Products.Add(new Product("DuckPro Ultra", 19000));
    
            stores.Add(new Store("Space Inc."));
            stores[1].ProductsGroups.Add(new ProductsGroup("Mobile phones"));
            stores[1].ProductsGroups[0].Products.Add(new Product("Rocket 1A", 900));
            stores[1].ProductsGroups[0].Products.Add(new Product("Rocket 2X", 3999));
            stores[1].ProductsGroups[0].Products.Add(new Product("Rocket 3E", 20000));
            stores[1].ProductsGroups.Add(new ProductsGroup("Notebooks"));
            stores[1].ProductsGroups[1].Products.Add(new Product("Shuttle 1A", 9999.99));
            stores[1].ProductsGroups[1].Products.Add(new Product("Shuttle 1X", 14000));
            stores[1].ProductsGroups[1].Products.Add(new Product("Shuttle Pro", 20000));
            stores[1].ProductsGroups.Add(new ProductsGroup("Computers"));
            stores[1].ProductsGroups[2].Products.Add(new Product("IssPC 2D", 10000.99));
            stores[1].ProductsGroups[2].Products.Add(new Product("IssPro 2X", 15000));
            stores[1].ProductsGroups[2].Products.Add(new Product("IssPro Pro", 19000));
    
            stores.Add(new Store("Fruit Inc."));
            stores[2].ProductsGroups.Add(new ProductsGroup("Mobile phones"));
            stores[2].ProductsGroups[0].Products.Add(new Product("Pineapple 1", 2900));
            stores[2].ProductsGroups[0].Products.Add(new Product("Mango 1", 3099));
            stores[2].ProductsGroups[0].Products.Add(new Product("Orange 1", 5000));
            stores[2].ProductsGroups.Add(new ProductsGroup("Notebooks"));
            stores[2].ProductsGroups[1].Products.Add(new Product("Mandarin X", 9999.99));
            stores[2].ProductsGroups[1].Products.Add(new Product("Lemon X", 14000));
            stores[2].ProductsGroups[1].Products.Add(new Product("Lemon Pro", 20000));
            stores[2].ProductsGroups.Add(new ProductsGroup("Computers"));
            stores[2].ProductsGroups[2].Products.Add(new Product("Plum X", 10000.99));
            stores[2].ProductsGroups[2].Products.Add(new Product("Plum Z", 15000));
            stores[2].ProductsGroups[2].Products.Add(new Product("Plum Pro", 19000));
    
            return stores;
        }
    }
    

     The below-mentioned code snippet binds the TreeView control to these classes.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        C1TreeView1.Columns.Clear()
        C1TreeView1.DataSource = Nothing
        C1TreeView1.DataMember = "\ProductsGroups\Products"
        Dim column = New C1.Win.TreeView.C1TreeColumn()
        column.HeaderText = "Name"
        C1TreeView1.Columns.Add(column)
        column = New C1.Win.TreeView.C1TreeColumn()
        column.DisplayFieldName = "CountOfProducts\CountOfProducts\"
        column.HeaderText = "Products in store"
        C1TreeView1.Columns.Add(column)
        column = New C1.Win.TreeView.C1TreeColumn()
        column.DisplayFieldName = "\\Price"
        column.HeaderText = "Price"
        C1TreeView1.Columns.Add(column)
        C1TreeView1.DataSource = StoreCollection.GetData()
    End Sub
    
    public Form1()
    {
        InitializeComponent();
    
        c1TreeView1.Columns.Clear();
        c1TreeView1.DataSource = null;
        c1TreeView1.DataMember = "\\ProductsGroups\\Products";
        var column = new C1.Win.TreeView.C1TreeColumn();
        column.HeaderText = "Name";
        c1TreeView1.Columns.Add(column);
        column = new C1.Win.TreeView.C1TreeColumn();
        column.DisplayFieldName = "CountOfProducts\\CountOfProducts\\";
        column.HeaderText = "Products in store";
        c1TreeView1.Columns.Add(column);
        column = new C1.Win.TreeView.C1TreeColumn();
        column.DisplayFieldName = "\\\\Price";
        column.HeaderText = "Price";
        c1TreeView1.Columns.Add(column);
        c1TreeView1.DataSource = StoreCollection.GetData();
    }
    

     Run the code to observe the following output.

    Heirarchical Data