RadControls for WinForms

RadTreeView binds to any object that implements IList, IListSource or IBindingList. This includes generic lists and BindingSource for example. To make databinding work, minimally you must assign the DataSource property of the treeview. 

Set the DisplayMember to the name of the field that you want visible in the treeview and the ValueMember to the name of a field that is stored in each node's Value property.

Minimal Example

The minimal example below binds a generic list of "Product" objects and displays only a single level of data. "Product" has an integer field, a float field and a string field. In this example, we set the ValueMember to the int field and the DisplayMember to the string field:

Copy[C#]
public partial class DataBindingBasics : Form
{
    public DataBindingBasics()
    {
        InitializeComponent();

        List<Product> products = new List<Product>();
        products.Add(new Product(567, "Bicycle", 5));
        products.Add(new Product(456, "Car", 5000));
        products.Add(new Product(789, "Bike", 1500));
        radTreeView1.DataSource = products;
        radTreeView1.DisplayMember = "Description";
        radTreeView1.ValueMember = "ID";      
    }
}
public class Product
{
    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 Product(int id, string description, float price)
    {
        _id = id;
        _description = description;
        _price = price;
    }
}
Copy[VB.NET]
Public Class DataBindingBasics
    Public Sub New()
        InitializeComponent()

        Dim products As New List(Of Product)()
        products.Add(New Product(567, "Bicycle", 5))
        products.Add(New Product(456, "Car", 5000))
        products.Add(New Product(789, "Bike", 1500))
        RadTreeView1.DisplayMember = "ID"
        RadTreeView1.DataSource = products
    End Sub
End Class

Public Class Product

    Private _id As Integer
    Private _description As String
    Private _price As Single

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    Public Property Price() As Single
        Get
            Return _price
        End Get
        Set(ByVal value As Single)
            _price = value
        End Set
    End Property

    Public Sub New(ByVal id As Integer, ByVal description As String, ByVal price As Single)
        _id = id
        _description = description
        _price = price
    End Sub
End Class

Getting the data bound object

To extend the previous minimal example, let's get the price of a product when we select the node of that product. Obviously, RadTreeNode contains information only about the ID and the Description of a Product. So, in order to be able to retrieve the Price value, we need to get the DataBoundItem of the RadTreeNode as shown below:

Copy[C#]
void radTreeView1_SelectedNodeChanged(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
{
    Product product = e.Node.DataBoundItem as Product;
    if (product != null)
    {
        MessageBox.Show("Product: " + e.Node.Text + ", Price: " + product.Price);
    }
}
Copy[VB.NET]
Private Sub RadTreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RadTreeViewEventArgs)
    Dim product As Product = TryCast(e.Node.DataBoundItem, Product)
    If product IsNot Nothing Then
        MessageBox.Show("Product: " & e.Node.Text & ", Price: " & product.Price)
    End If
End Sub
As a result, when we select a node, we will get a messagebox showing the price of the product related to the currently selected node:

treeview-data-binding-data-binding-basics 001

Binding and Displaying Hierarchical data

The great value of RadTreeView is the ability to display hierarchical data. It supports binding to and displaying Database hierarchy and hierarchy of custom objects which have member collections of related objects (ORM generated classes for example).

The next example extends the previous examples to include a new "Category" parent level where each category contains multiple products. "Categories" has a single "Name" property and a list of products.

treeview-data-binding-data-binding-basics 002

In order to display the hierarchy of business objects, we just need to set appropriate DisplayMember and ChildMember values. The DisplayMember value is built from the display members of the respective objects joined with the '\\' symbol ('\' for VB.NET). The ChildMember is built from the names of the properties that represent the collections of subobjects. Just like in the case of the DisplayMember, the names of these properties are joined with the '\\' symbol ('\' for VB.NET). The first member of the ChildMember string should represent the parent level of hierarchy and you can set it to a value of your choice. For example, in our case we set this member to "Categories", but we could also set it to "MyCategories":

Copy[C#]
public partial class BasicsHierarchyForm : Form
{
    public BasicsHierarchyForm()
    {
        InitializeComponent();

        List<Product> products = new List<Product>();
        products.Add(new Product(567, "Bicycle", 5));
        products.Add(new Product(456, "Car", 5000));
        products.Add(new Product(789, "Bike", 1500));
        List<Category> categories = new List<Category>();
        categories.Add(new Category("Bikes", products));
        categories.Add(new Category("Accessories", null));
        categories.Add(new Category("Clothing", null));
        radTreeView1.DataSource = categories;
        radTreeView1.DisplayMember = "Name\\Description";
        radTreeView1.ChildMember = "Categories\\Products";    
    }

    void radTreeView1_SelectedNodeChanged(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
    {
        Product product = e.Node.DataBoundItem as Product;
        if (product != null)
        {
            MessageBox.Show("Product: " + e.Node.Text + ", Price: " + product.Price);
        }
    }
}
public class Product
{
    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 Product(int id, string description, float price)
    {
        _id = id;
        _description = description;
        _price = price;
    }
}
public class Category
{
    public Category(string name, List<Product> products)
    {
        _name = name;
        _products = products;
    }
    private List<Product> _products;
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public List<Product> Products
    {
        get { return _products; }
        set { _products = value; }
    }
}
Copy[VB.NET]
Public Class BasicsHierarchyForm
    Public Sub New()
        InitializeComponent()

        Dim products As New List(Of Product)()
        products.Add(New Product(567, "Bicycle", 5))
        products.Add(New Product(456, "Car", 5000))
        products.Add(New Product(789, "Bike", 1500))
        Dim categories As New List(Of Category)()
        categories.Add(New Category("Bikes", products))
        categories.Add(New Category("Accessories", Nothing))
        categories.Add(New Category("Clothing", Nothing))
        RadTreeView1.DataSource = categories
        RadTreeView1.DisplayMember = "Name\Description"
        RadTreeView1.ChildMember = "Categories\Products"
    End Sub

     Private Sub RadTreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RadTreeViewEventArgs)
        Dim product As Product = TryCast(e.Node.DataBoundItem, Product)
        If product IsNot Nothing Then
            MessageBox.Show("Product: " & e.Node.Text & ", Price: " & product.Price)
        End If
    End Sub
End Class

    Public Class Product

        Private _id As Integer
        Private _description As String
        Private _price As Single

        Public Property ID() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        Public Property Description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        Public Property Price() As Single
            Get
                Return _price
            End Get
            Set(ByVal value As Single)
                _price = value
            End Set
        End Property

        Public Sub New(ByVal id As Integer, ByVal description As String, ByVal price As Single)
            _id = id
            _description = description
            _price = price
        End Sub
    End Class

    Public Class Category
        Public Sub New(ByVal name As String, ByVal products As List(Of Product))
            _name = name
            _products = products
        End Sub
        Private _products As List(Of Product)
        Private _name As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Public Property Products() As List(Of Product)
            Get
                Return _products
            End Get
            Set(ByVal value As List(Of Product))
                _products = value
            End Set
        End Property
    End Class