Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q1 2012 released 04/11/2012
select

TreeView / Programmatic Data Binding

  • DataSet (Northwind Employees)
    • Fuller
      • Davolio
      • Leverling
      • Peacock
      • Buchanan
        • Suyama
        • King
        • Dodsworth
      • Callahan
  • IEnumerable
    • Products
      • RadControls for ASP.NET Ajax
        • RadGrid
        • RadScheduler
        • RadEditor
      • RadControls for Silverlight
        • RadGrid
        • RadMenu
        • RadEditor

  • RadTreeView fully supports binding to a wide range of datasources:

    • DataSet, DataTable, DataView
    • ASP 2.0 DataSource types including
      • Hierarchical DataSource components
      • Table-based DataSource components
      • ObjectDataSource
    • Any object that implements the IEnumerable interface

    In addition, while not strictly data binding, RadTreeView can load XML content files. You can easily populate your treeview instance with only a few lines of code. The built-in data binding supports auto-binding from a a single self-referencing table with ID -> ParentID relation.

    Below are the properties and methods related to data binding:

    DataSource - Set to an instance of your data source. This is mandatory when binding the RadTreeView at runtime
    DataSourceID - Set to the ID of your data source. This is mandatory when binding the RadTreeView declaratively
    DataMember - If the data source is a DataSet and DataMember is set, then the RadTreeView is bound to the DataTable with the respective name in the DataSet. If DataMember is not set, the RadTreeView is bound to the first DataTable in the DataSet
    DataFieldID - This is the field name from the data source used to uniquely identify each row. This field is required when binding to hierarchical data
    DataFieldParentID - This is the field name from the data source used to identify the row for the parent node. This field is required when binding to hierarchical data
    DataTextField - This is the field name from the data source that populates each node's Text property during binding
    DataValueField - This is the field name from the data source that populates each node's Value property during binding
    DataNavigateUrlField - This is the field name from the data source that populates each node's NavigateUrlField property during binding
    DataBind - Call this method after you have set the aforementioned properties when binding at runtime. This method is mandatory for binding at runtime
    MaxDataBindDepth - This property is used to determine the binding depth. If for example you want to bind only the first two levels of the treeview, you should set this property to 2. The default value of the MaxDataBindDepth property is -1, which means that all nodes will be bound. Marking the treeview instance with MaxDataBindDepth="0" will NOT bind any nodes.
    AppendDataBoundNodes - If you bind the treeview using the DataBind method, all TreeNodes are automatically cleared. Setting AppendDataBoundNodes to True preserves the nodes that are already present in the treeview. This lets you bind RadTreeView to multiple data sources or use both unbound and bound modes.

    If you need to map additional fields from the Data Source to properties of the RadTreeView item, you can use the NodeDataBound event.

    [C#]
    protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)
    {
    e.Node.ToolTip = (e.Node.DataItem as DataRowView)["Description"].ToString();
    }


    [VB]
    Protected Sub RadTreeView1_NodeDataBound(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)
    e.Node.ToolTip = (TryCast(e.Node.DataItem, DataRowView))("Description").ToString()
    End Sub

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>

    <%@ Page AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.TreeView.Databinding.DefaultCS"
        Language="c#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <qsf:HeadTag runat="server" ID="Headtag1"></qsf:HeadTag>
        <style type="text/css">
            ul.treeview-list
            {
                list-style: none;
                padding: 0;
                margin: 0;
            }
            li.treeview-item
            {
                float: left;
                width: 338px;
                padding-right: 4px;
            }
            div.text
            {
                font: 13px 'Segoe UI' , Arial, sans-serif;
                color: #4888a2;
                padding: 6px 18px;
                display: block;
            }
        </style>
    </head>
    <body class="BODY">
        <form runat="server" id="mainForm" method="post">
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
        <qsf:Header runat="server" ID="Header1" NavigationLanguage="C#"></qsf:Header>
        <ul class="treeview-list">
            <li class="treeview-item" style="border: none">
                <div class="text">
                    DataSet (Northwind Employees)</div>
                <telerik:RadTreeView ID="RadTreeView1" runat="server" Width="300px" Height="250px">
                    <DataBindings>
                        <telerik:RadTreeNodeBinding Expanded="True" />
                    </DataBindings>
                </telerik:RadTreeView>
            </li>
            <li class="treeview-item">
                <div class="text">
                    IEnumerable</div>
                <telerik:RadTreeView ID="RadTreeView2" runat="server" Width="300px" Height="250px">
                    <DataBindings>
                        <telerik:RadTreeNodeBinding Expanded="True" />
                    </DataBindings>
                </telerik:RadTreeView>
            </li>
        </ul>
        <qsf:Footer runat="server" ID="Footer1"></qsf:Footer>
        </form>
    </body>
    </html>

Get more than expected!

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2012 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451