ComponentOne TreeView for WinForms
Binding TreeView to Lists
Data Binding > Binding TreeView to Lists

You can bind TreeView to a list containing objects that are accessed by using index values.

Here is a sample code that first creates a class Products and creates an instance of List<T> belonging to System.Collections.Generic. Then it binds the TreeView control to the list and displays the data from the list in a hierarchical form when the Data Generate button is clicked.

The following code snippet creates a class Products.cs.

' create a class 
Public Class Products

    Private _id As Integer
    Private _description As String
    Private _price As Single
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set
            _id = Value
        End Set
    End Property
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set
            _description = Value
        End Set
    End Property
    Public Property Price() As Single
        Get
            Return _price
        End Get
        Set
            _price = Value
        End Set
    End Property
    Public Sub New(id As Integer, description As String, price As Single)
        _id = id
        _description = description
        _price = price
    End Sub
End Class
// create a class 
public class Products
{

    private int _id;
    private string _description;
    private float _price;
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }
    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
    public float Price
    {
        get { return _price; }
        set { _price = value; }
    }
    public Products(int id, string description, float price)
    {
        _id = id;
        _description = description;
        _price = price;
    }
}

The following code snippets binds TreeView to the list.

' to generate data when the button is clicked
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' create a list
    Dim product As New List(Of Products)()

    ' add the objects to the list
    product.Add(New Products(567, "Bicycle", 5))
    product.Add(New Products(456, "Car", 5000))
    product.Add(New Products(789, "Bike", 1500))

    ' bind TreeView to the list
    C1TreeView1.DataSource = product

    ' set the field to be displayed in the column
    C1TreeView1.Columns(0).DisplayFieldName = "Description\Description"
End Sub
// to generate data when the button is clicked
private void button1_Click(object sender, EventArgs e)
{
    // create a list
    List<Products> product = new List<Products>();

    // add the objects to the list
    product.Add(new Products(567, "Bicycle", 5));
    product.Add(new Products(456, "Car", 5000));
    product.Add(new Products(789, "Bike", 1500));

    // bind TreeView to the list
    c1TreeView1.DataSource = product;

    // set the field to be displayed in the column
    c1TreeView1.Columns[0].DisplayFieldName = "Description\\Description";
}

Once you have clicked the Data Generate button after running the code, the following output appears.