# 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](https://www.telerik.com/products/winforms/documentation/assets/9ad02878d926686a17991c71cbf25723/treeview-data-binding-binding-to-database-data001.png)

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](https://www.telerik.com/products/winforms/documentation/assets/8a057616f2998b0a2aaff2430dd1cd5d/treeview-data-binding-binding-to-database-data002.png)

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](https://www.telerik.com/products/winforms/documentation/assets/7f5441b4005226c3e7200634eb5aeb9d/treeview-data-binding-binding-to-database-data003.png)

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

   ![WinForms RadTreeView DataBase](https://www.telerik.com/products/winforms/documentation/assets/54e3f257d7273a2e7bf5dc86a4fd1de9/treeview-data-binding-binding-to-database-data004.png)

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.

```C#
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"));
```

```VB.NET
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

* [Binding to Object-relational Data](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/binding-to-object-relational-data)

* [Binding to Self Referencing Data](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/binding-to-self-referencing-data)

* [Binding to XML Data](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/binding-to-xml-data)

* [Data Binding](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/overview)

* [Binding CheckBoxes](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/databinding-checkboxes)

* [Serialize/Deserialize to XML](https://www.telerik.com/products/winforms/documentation/controls/treeview/data-binding/deserialize-to-xml)
