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

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)
WinForms RadGridView Two-Level Hierarchy Tutorial

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.


WinForms RadGridView AutoGenerateHierarchy

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:

WinForms RadGridView Multi-Level Hierarchy


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

WinForms RadGridView AutoGenerateHierarchy

See Also

In this article