# Building a Hierarchical Grid

This tutorial will walk you through creating a Web page that contains a hierarchical **RadGrid** control. It shows how to:

* Bind detail tables using a declarative data source.

* Bind detail tables dynamically, using the **DetailTableDataBind** event.

## Creating the data sources

1. Locate the "..\Live Demos\App_Data" folder underneath the folder where you have installed your controls. In this folder, find the **Northwind.mdf** file and copy it into the **App_Data** folder of your Web site. The **Solution Explorer** for your application should look something like the following:

   ![grid gettingstarted 1](https://www.telerik.com/products/aspnet-ajax/documentation/assets/6d9eb63dcf4bf25ec7bc813ab598b09a/grid_gettingstarted1.png)

2. Drag and drop a **RadGrid** control from the toolbox onto your Web page:

   ![grid gettingstarted 2](https://www.telerik.com/products/aspnet-ajax/documentation/assets/83f915d5db8425f3951314dfedba9d1c/grid_gettingstarted2.jpg)

3. Using the **RadGrid's** [Smart Tag](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/smarttag) expand the drop-down list labelled Choose Data Source and select **<New data source...>**:

   ![grid gettingstarted 03](https://www.telerik.com/products/aspnet-ajax/documentation/assets/47a3804d3838c51d289296bf8e3497dd/grid_gettingstarted03.jpg)

4. The **Data Source Configuration Wizard** appears. On the **Choose a Data Source Type** page, select **SQL Database** and click **OK**:

   ![grid gettingstarted 4](https://www.telerik.com/products/aspnet-ajax/documentation/assets/28ec90d2bef22ec3fb828ac9afe4d068/grid_gettingstarted4.png)

5. On the **Choose Your Data Connection** page, expand the drop-down list by clicking the little triangle on the right side and select the **Northwind.mdf** from the available options. Then click the **Next** button:

   ![grid gettingstarted 5](https://www.telerik.com/products/aspnet-ajax/documentation/assets/78aedcf367985110b6b21365ae3e2e84/grid_gettingstarted5.png)

   > note If you encounter a "Database schema could not be retrieved" exception, follow the [Visual Studio 2012 data source configuration guide](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/visual-studio-2012-datasource-configuration).

6. On the **Save the Connection String to the Application Configuration File** page, set name for the **Connection String** and then press **Next**:

   ![grid gettingstarted conn String](https://www.telerik.com/products/aspnet-ajax/documentation/assets/e4926073c140c9e40973827cfc9eb2bc/grid_gettingstarted_connstring.png)

7. On the **Configure the Select Statement** page, select the **Customers** table from the drop-down list, and select all **(\*)** fields. Then choose **Next**:

   ![Configure Select Statement](https://www.telerik.com/products/aspnet-ajax/documentation/assets/f53f7b3843ea9970488c9c0782e3d991/grid_hierarchy6.png)

8. On the **Test Query** page, click the **Test Query** button to see the data. Then click **Finish** to exit the **Configure Data Source** wizard:

   ![grid hierarchy 7](https://www.telerik.com/products/aspnet-ajax/documentation/assets/e20660eea8234b31587dc784189e1893/grid_hierarchy7.png)

9. Using **RadGrid's** [Smart Tag](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/smarttag) add a second **DataSource** component to RadGrid.

   ![Select DataSource](https://www.telerik.com/products/aspnet-ajax/documentation/assets/f9bd46e0f81e73b364341ef4618789d3/grid_hierarchy4_1.png)

10. Choose the same database on the **Choose Your Data Connection** page of the wizard. Then click the **Next** button:

    ![Choose DataBase](https://www.telerik.com/products/aspnet-ajax/documentation/assets/99ae6d98e0baafe84b2700cbac9aee18/grid_hierarchy7_1.png)

11. On the **Configure the Select Statement** page, choose the **Orders** table and select all columns again. This time, we are creating a data source for a detail table, so we need to add a where clause so the detail records are limited to the ones that match a record in the parent table. Click the **Where...** button to add a **WHERE** clause:

    ![Second Select Statement](https://www.telerik.com/products/aspnet-ajax/documentation/assets/79783def4860fc444c17e4df3c5d7c6a/grid_hierarchy8.png)

12. On the **Add WHERE Clause** page, choose the **CustomerID** column, and select **None** as the source of the parameter. Then click the **Add** button to add the condition to the **Where** clause, and click **OK**.

    ![Where clause](https://www.telerik.com/products/aspnet-ajax/documentation/assets/8004f9e6d4cf1c14d483aaaac1a6c03d/grid_hierarchy9.png)

13. The **Configure Select Statement** page now shows your WHERE clause. Click the **Next** button, and then, on the next page, choose **Finish**:

    ![Configure Select Statement](https://www.telerik.com/products/aspnet-ajax/documentation/assets/c5d174c33d52220cb78fc57644250d6a/grid_hierarchy10.png)

14. After you press the **Finish** button **RadGrid** will ask you if you want to apply the new datasource schema, choose **No**:

    ![Apply New Schema](https://www.telerik.com/products/aspnet-ajax/documentation/assets/df371b443df032c082b30c7120cd4e7e/grid_hierarchy11.png)

## Creating the grid and binding its Master Table

1. In the **RadGrid's** Smart Tag, select **SQLDataSource1** as a data source for the grid. This binds the **MasterTableView** to the **Customers** table. If RadGrid asks you whether to apply the new datasource schema, choose **Yes**. In addition, while in the Smart Tag, choose **Metro** from the **Skin** drop-down to give the grid a different skin:

   ![grid addcontrols 12](https://www.telerik.com/products/aspnet-ajax/documentation/assets/65df1b2b491c7d8c04c69950ff826fbb/grid_addcontrols12.png)

2. Still in the Smart Tag, click the link labelled **Open Configuration Wizard**:

   ![grid gettingstarted 10](https://www.telerik.com/products/aspnet-ajax/documentation/assets/682d7d6cd233a9c1148d47ae38d22c28/grid_gettingstarted10.jpg)

3. In the [ RadGrid Editor](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/setting-radgrid),

   1. Select the **MasterTableView** in the **Grid hierarchy** tree view that you find in the upper left corner.

   2. Select **Columns** tab.

   3. Remove any columns you do not want to display for the Master Table:

   ![DeleteColumn](https://www.telerik.com/products/aspnet-ajax/documentation/assets/49c72ff0c1c5bd372798b8b9a5b107cf/grid_hierarchy13.png)

4. In **General** tab of the **MasterTableView** node make sure that the **DataKeyNames** list contains the string **CustomerID**. This column must be identified as a key name so that it can be used to link to detail tables:

   ![SetDataKeyNames](https://www.telerik.com/products/aspnet-ajax/documentation/assets/906dca0e70434f5abea50b26ff1d7701/grid_hierarchy14.png)

## Creating and binding the first detail table

1. In the **Grid hierarchy** tree view, click the "+" button while the **MasterTableView** is selected to add a Detail TableView.

   ![Add Detail Table](https://www.telerik.com/products/aspnet-ajax/documentation/assets/0c4fb0362eb5b44a043f4aa0509f8f9d/grid_hierarchy15.png)

2. Select the detail table under **MasterTableView** node.

3. In the right pane, set the **DataSourceID** property to **SqlDataSource2** to bind it to the **Orders** table.

4. In the **Parent table relation**, fill in the **CustomerID** string in both fields(**MasterKeyField and DetailKeyField**) click the "+" button to add a relationship between a field in the detail table and its corresponding field in the parent table.

5. Now set the **DataKeyNames** property of the detail table to **OrderID**. This will be useful later when binding a second detail table as a child of this table view.

   ![Set Parent Table Relation](https://www.telerik.com/products/aspnet-ajax/documentation/assets/4eaaf6a76281434f6d1033a94324dd5a/grid_hierarchy16.png)

6. Before exiting the **RadGrid** Editor, return to the **MasterTableView** node, on the right pane click the **Paging** tab and then check the **Allow paging** checkbox and set the **Page size** to **5** rows:

   ![Set Paging](https://www.telerik.com/products/aspnet-ajax/documentation/assets/60d14b67f0f6c4dc550cc5e3e9175116/grid_hierarchy17.png)

7. Run the application to see that the grid now displays a two-level hierarchy, with the **Orders** table nested inside the **Customers** table:

   ![Hierarchical RadGrid](https://www.telerik.com/products/aspnet-ajax/documentation/assets/8a6332a623904b47fc4456ba503e1d98/grid_hierarchy18.png)

## Creating and binding the second detail table

1. Bring up the **RadGrid's** Editor again and add a second Detail Table as a child of the first Detail Table:

   ![Add Second Detail Table](https://www.telerik.com/products/aspnet-ajax/documentation/assets/6b8669db187f324c392a425b7adacb48/grid_hierarchy19.png)

2. Because we will be binding the detail table dynamically, there is no need to set any more properties, so exit the property builder:

   ![Close the Editor](https://www.telerik.com/products/aspnet-ajax/documentation/assets/ac24f6f8c4e46e3707493000412ccee9/grid_hierarchy20.png)

3. Give the **RadGrid** a **DetailTableDataBind** event handler. This event handler

   1. Checks if the detail table has a datasource already.

   2. Creates a SELECT command based on the current value of the data key from the parent table (This is available because we set the **DataKeyNames** property of the parent table).

   3. Fills a **DataTable** using the dynamically created SELECT command.

   4. Assigns the data table as the **DataSource** of the detail table view we are binding:

```C#
protected void RadGrid1_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
	if (e.DetailTableView.DataSourceID == "")
	{
		GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
		string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
		e.DetailTableView.DataSource = GetDataTable("Select * from [Order Details] where OrderID = '" + OrderID + "'");
	}
}

public DataTable GetDataTable(string query)
{
	String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
	SqlDataAdapter adapter = new SqlDataAdapter();
	DataTable myDataTable = new DataTable();
	using (SqlConnection conn = new SqlConnection(ConnString))
	{
		adapter.SelectCommand = new SqlCommand(query, conn);
		adapter.Fill(myDataTable);
	}
	return myDataTable;
}
```

```VB
Private Sub RadGrid1_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind
	If e.DetailTableView.DataSourceID = "" Then
		Dim dataItem As GridDataItem = DirectCast(e.DetailTableView.ParentItem, GridDataItem)
		Dim OrderID As String = dataItem.GetDataKeyValue("OrderID").ToString()
		e.DetailTableView.DataSource = GetDataTable("Select * from [Order Details] where OrderID = '" + OrderID + "'")
	End If
End Sub

Public Function GetDataTable(query As String) As DataTable
	Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
	Dim adapter As New SqlDataAdapter()
	Dim myDataTable As New DataTable()
	Using conn As New SqlConnection(ConnString)
		adapter.SelectCommand = New SqlCommand(query, conn)
		adapter.Fill(myDataTable)
	End Using
	Return myDataTable
End Function
```

Run the application. Note that you can now see the dynamically bound Detail Table:
![RadGrid With Three Levels](https://www.telerik.com/products/aspnet-ajax/documentation/assets/25c8cf171a68c695205b093f1d4908d4/grid_hierarchy21.png)

## See Also

* [Creating hierarchical grids from design time](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/creating-hierarchical-grids-from-design-time)
* [Declarative data source](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/design-time/declarative-data-source)
