New to Telerik UI for WinForms? Download free 30-day trial

Binding to Database Data

RadTreeView supports binding to Database data. Levels are created using the RadTreeView RelationBindings collection. Each RelationBinding object describes the parent data being bound to, the child data being bound to and the data columns used to populate Text and Value properties of the nodes. The RelationBinding constructor has several overrides. Minimally RelationName and DataSource must be passed to the Add() method. The example shown below includes the following parameters:

  • DataSource: The data source object being bound to. In this example the data source is the generic list of Category objects.

  • DataMember: This is a name of a specific record set within the DataSource. Only one set of records exists in this example so the passed parameter is null.

  • DisplayMember: The name of the field that populates the Text property of each node. 

  • ValueMember: The name of the data field that populates the Value property of each node.

  • ParentMember: The name of the parent data field which takes part in the relation.

  • ChildMember: The name of the child data field which takes part in the relation.

WinForms RadTreeView Binding

The purpose of this example is to demonstrate how to bind to database data.

  1. In a new Windows project drop a RadTreeView control on the form.

  2. In the DataSource property drop down in the Properties Window select the Add Project Data Source link.

  3. In the Choose a Data Source Type page select the Database icon, then click Next.

    WinForms RadTreeView Wizard

  4. In the Choose Your Data Connection page of the wizard click New Connection.

  5. In the Add Connection dialog click the Change button, select "Microsoft Access Database File" and click the OK button. In the Database file name entry click the Browse button and locate the MusicCollection.mdb file in the installation directory under \Examples\DataSources. Click OK. When prompted if you would like to copy the local data file to your project click Yes.

    WinForms RadTreeView Connection

  6. In the Choose Your Database Objects page of the wizard select the "Artists", "Albums" and "Songs" tables checkboxes. Click Finish.

    WinForms RadTreeView DataBase

  7. Set the DataSource property first to Songs, then to Albums and finally to Artists data table. This will create the necessary data-binding components in the component tray - a DataSet, BindingSources and TableAdapters. The DataSet is the container for the data, the TableAdapters are used to fill the DataSet.

  8. In the form's Load event handler add the code shown below.

this.radTreeView1.DataSource = this.artistsBindingSource;
this.radTreeView1.DisplayMember = "ArtistName";
this.radTreeView1.ValueMember = "ArtistID";
this.radTreeView1.RelationBindings.Add(new RelationBinding(this.albumsBindingSource, "AlbumName", "ArtistID", "ArtistID", "AlbumID"));
this.radTreeView1.RelationBindings.Add(new RelationBinding(this.songsBindingSource, "SongName", "AlbumID", "AlbumID", "SongID"));

Me.RadTreeView1.DataSource = Me.ArtistsBindingSource
Me.RadTreeView1.DisplayMember = "ArtistName"
Me.RadTreeView1.ValueMember = "ArtistID"
Me.RadTreeView1.RelationBindings.Add(New RelationBinding(Me.AlbumsBindingSource, "AlbumName", "ArtistID", "ArtistID", "AlbumID"))
Me.RadTreeView1.RelationBindings.Add(New RelationBinding(Me.SongsBindingSource, "SongName", "AlbumID", "AlbumID", "SongID"))

See Also

In this article