New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Binding to DataTable

RadTreeView can be bound to a DataTable, DataSet and a DataView. This example shows binding to a DataTable object.

The declaration of the RadTreeView object includes no DataSourceID property or <Items> section:

<telerik:RadTreeView RenderMode="Lightweight" ID="RadTreeView1" runat="server" Skin="WebBlue" OnNodeDataBound="RadTreeView1_NodeDataBound">

In the Page_Load event handler, create and fill the DataTable object, then bind it to the RadTreeView. You must call the DataBind method after setting the DataSource property. Minimally you must assign the DataSource and DataTextField properties. Optionally, you can assign DataFieldID and DataValueField properties. The following example binds to a table but does not create a hierarchy. See the Binding to Hierarchical Data and Getting Started, Binding to a Database for more information.

RadTreeView DataBase

using System.Data.SqlClient;using Telerik.Web.UI;
namespace RadTreeView_DataBindDataTable
public partial class _Default : System.Web.UI.Page        
    protected void Page_Load(object sender, EventArgs e)                
        if (!IsPostBack)                        
    private void BindToDataTable(RadTreeView treeView)           
        SqlConnection connection = new SqlConnection(Properties.Settings.Default.NwindConnectionString);        
        SqlDataAdapter adapter = new SqlDataAdapter("select CategoryID, CategoryName, Description from Categories", connection);            
        DataTable dataTable = new DataTable();        
        treeView.DataTextField = "CategoryName";                  
        treeView.DataFieldID = "CategoryID";                   
        treeView.DataValueField = "Description";                     
        treeView.DataSource = dataTable;                    
Imports System.Data.SqlClient
Imports Telerik.Web.UI
Imports System.Data
Namespace RadTreeView_DataBindDataTable
    Partial Public Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If (Not IsPostBack) Then
            End If
        End Sub
        Private Sub BindToDataTable(ByVal treeView As RadTreeView)
            Dim connection As New SqlConnection(Properties.Settings.[Default].NwindConnectionString)
            Dim adapter As New SqlDataAdapter("select CategoryID, CategoryName, Description from Categories", connection)
            Dim dataTable As New DataTable()
            treeView.DataTextField = "CategoryName"
            treeView.DataFieldID = "CategoryID"
            treeView.DataValueField = "Description"
            treeView.DataSource = dataTable
        End Sub
    End Class
End Namespace

See Also

In this article