New to Telerik UI for WinForms? Start a free 30-day trial
Build GridView Hierarchy with Multiple Tabs
Updated over 6 months ago
Environment
| Product Version | Product | Author |
|---|---|---|
| 2022.2.622 | RadGridView for WinForms | Desislava Yordanova |
Description
This article shows how to setup a hierarchical RadGridView that contains a child level with two tabs:

Solution
It is necessary to add a GridViewTemplate to the MasterTemplate.Templates collection for each child tab. Then, for each child template, define a GridViewRelation linking the master and child level via the respective ID property.
C#
private void RadForm1_Load(object sender, EventArgs e)
{
List<Category> categories = new List<Category>()
{
new Category(1, "Category 1"),
new Category(2, "Category 2"),
new Category(3, "Category 3"),
new Category(4, "Category 4")
};
radGridView1.DataSource = categories;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
List<Product> products = new List<Product>()
{
new Product(1, 1, "Product 1"),
new Product(2, 1, "Product 2"),
new Product(3, 2, "Product 3"),
new Product(4, 1, "Product 4"),
new Product(5, 3, "Product 5"),
new Product(6, 2, "Product 6"),
new Product(7, 2, "Product 7"),
new Product(8, 2, "Product 8"),
new Product(9, 3, "Product 9"),
new Product(10, 3, "Product 10"),
new Product(11, 1, "Product 11"),
new Product(12, 1, "Product 12")
};
GridViewTemplate firstChildTemplate = new GridViewTemplate();
firstChildTemplate.DataSource = products;
firstChildTemplate.Caption = "First child template";
firstChildTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.MasterTemplate.Templates.Add(firstChildTemplate);
GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
relation.ChildTemplate = firstChildTemplate;
relation.RelationName = "CategoriesProducts";
relation.ParentColumnNames.Add("CategoryID");
relation.ChildColumnNames.Add("CategoryID");
radGridView1.Relations.Add(relation);
List<Supplier> suppliers = new List<Supplier>()
{
new Supplier(1, 2, "Supplier 1"),
new Supplier(2, 4, "Supplier 2"),
new Supplier(3, 2, "Supplier 3"),
new Supplier(4, 1, "Supplier 4"),
new Supplier(5, 4, "Supplier 5"),
new Supplier(6, 2, "Supplier 6"),
new Supplier(7, 4, "Supplier 7"),
new Supplier(8, 2, "Supplier 8"),
new Supplier(9, 4, "Supplier 9")
};
GridViewTemplate secondChildTemplate = new GridViewTemplate();
secondChildTemplate.DataSource = suppliers;
secondChildTemplate.Caption = "Second child template";
secondChildTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.MasterTemplate.Templates.Add(secondChildTemplate);
GridViewRelation relation2 = new GridViewRelation(radGridView1.MasterTemplate);
relation2.ChildTemplate = secondChildTemplate;
relation2.RelationName = "SuppliersCategories";
relation2.ParentColumnNames.Add("CategoryID");
relation2.ChildColumnNames.Add("CategoryID");
radGridView1.Relations.Add(relation2);
}
public class Category
{
public int CategoryID
{
get
{
return m_CategoryID;
}
set
{
m_CategoryID = value;
}
}
private int m_CategoryID;
public string CategoryName
{
get
{
return m_CategoryName;
}
set
{
m_CategoryName = value;
}
}
private string m_CategoryName;
public Category(int categoryID, string categoryName)
{
this.CategoryID = categoryID;
this.CategoryName = categoryName;
}
}
public class Product
{
public int ProductID
{
get
{
return m_ProductID;
}
set
{
m_ProductID = value;
}
}
private int m_ProductID;
public int CategoryID
{
get
{
return m_CategoryID;
}
set
{
m_CategoryID = value;
}
}
private int m_CategoryID;
public string ProductName
{
get
{
return m_ProductName;
}
set
{
m_ProductName = value;
}
}
private string m_ProductName;
public Product(int productID, int categoryID, string productName)
{
this.ProductID = productID;
this.CategoryID = categoryID;
this.ProductName = productName;
}
}
public class Supplier
{
public int SupplierID
{
get
{
return m_SupplierID;
}
set
{
m_SupplierID = value;
}
}
private int m_SupplierID;
public int CategoryID
{
get
{
return m_CategoryID;
}
set
{
m_CategoryID = value;
}
}
private int m_CategoryID;
public string SupplierName
{
get
{
return m_SupplierName;
}
set
{
m_SupplierName = value;
}
}
private string m_SupplierName;
public Supplier(int supplierID, int categoryID, string supplierName)
{
this.SupplierID = supplierID;
this.CategoryID = categoryID;
this.SupplierName = supplierName;
}
}