Edit this page

Binding to Hierarchical Data Automatically

Generating two-level hierarchy

RELATED VIDEOS
RadGridView for WinForms Hierarchy Overview
In this video you will learn the various ways you can display hierarchical data in a RadGridView. (Runtime: 12:13)
gridview-hierarchical-grid-binding-to-hierarchical-data 001

At runtime, if the data source for the grid is a System.Data.DataSet type and there are relations defined in it, the hierarchy can be created automatically. Set the AutoGenerateHierarchy property true to get this behavior. In the example below, the Northwind dataset has Categories and Products joined by CategoryID.


gridview-hierarchical-grid-binding-to-hierarchical-data-automatically 004

The run-time code fills the categories and products data tables, sets the AutoGenerateHierarchy to true, assigns the data set containing both tables to the grid DataSource and the DataMember is the name of the parent table. The last three lines of code below can be configured at design time.


private void BindingToHierarchicalGridAutomatically_Load(object sender, EventArgs e)
{
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);
    this.categoriesTableAdapter.Fill(this.nwindDataSet.Categories);
    radGridView1.AutoGenerateHierarchy = true;
    radGridView1.DataSource = this.nwindDataSet;
    radGridView1.DataMember = "Categories";
}

Private Sub BindingToHierarchicalGridAutomatically_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'NwindDataSet.Order_Details' table. You can move, or remove it, as needed.
    Me.Order_DetailsTableAdapter.Fill(Me.NwindDataSet.Order_Details)
    'TODO: This line of code loads data into the 'NwindDataSet.Order_Details' table. You can move, or remove it, as needed.
    Me.ProductsTableAdapter.Fill(Me.NwindDataSet.Products)
    Me.CategoriesTableAdapter.Fill(Me.NwindDataSet.Categories)
    RadGridView1.AutoGenerateHierarchy = True
    RadGridView1.DataSource = Me.NwindDataSet
    RadGridView1.DataMember = "Categories"
End Sub

Generating multi-level hierarchy

It is possible to auto generate Multi-level hierarchy as well. You should again set the DataSource and AutoGenerateHierarachy properties. Here are the three data tables from the Northwind database, used in the code snippet to generate the three-level hierarchy:

gridview-hierarchical-grid-binding-to-hierarchical-data-automatically 002


public void Dummy()
{ 
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);
    this.categoriesTableAdapter.Fill(this.nwindDataSet.Categories);
    this.order_DetailsTableAdapter.Fill(this.nwindDataSet.Order_Details);
    radGridView1.DataSource = nwindDataSet;
    radGridView1.DataMember = "Categories";
    radGridView1.AutoGenerateHierarchy = true;
}

Public Sub Dummy()
    Me.ProductsTableAdapter.Fill(Me.NwindDataSet.Products)
    Me.CategoriesTableAdapter.Fill(Me.NwindDataSet.Categories)
    RadGridView1.DataSource = NwindDataSet
    RadGridView1.DataMember = "Categories"
    RadGridView1.AutoGenerateHierarchy = True
End Sub

gridview-hierarchical-grid-binding-to-hierarchical-data-automatically 003

See Also