In our previous blog post we showed how to use the new RadGridView Object-Relational Hierarchy with the Entity Framework. The same scenario can be easily implemented with Telerik OpenAccess ORM. The OpenAccess wizard for adding a new DomainModel will help us build the model of the well-known Northwind database in a few easy steps:
Once this is done, the auto-generation of the hierarchy is very simple and needs just a few lines of code. We only need the LINQ query to the root object used for a DataSource (in our case “Suppliers”) and then setting AutoGenerateHierarchy to true:
NorthwindEntities entities =
new
NorthwindEntities();
var query = from suppliers
in
entities.Suppliers select suppliers;
this
.radGridView1.DataSource = query.ToList();
this
.radGridView1.AutoGenerateHierarchy =
true
;
The result is self-explanatory:
Although this was nice and quick, this scenario does not take into account further presentation tweaking, because it was auto-generated. In cases when you need to fine-tune the visible data and its presentation, you will need to manually add the Object-Relational hierarchy. The key thing in this setup is to add a relation that contains only one ChildColumnNames, i.e. the name of the IEnumerable/IList property used for loading the second data level (which is always loaded on expand through lazy loading):
private
void
Form2_Load(
object
sender, EventArgs e)
{
NorthwindEntities entities =
new
NorthwindEntities();
var query = from customers
in
entities.Customers select customers;
GridViewTemplate childTemplate = CreateChildTemplate();
GridViewRelation relation =
new
GridViewRelation(
this
.radGridView1.MasterTemplate, childTemplate);
relation.ChildColumnNames.Add(
"Orders"
);
this
.radGridView1.Relations.Add(relation);
this
.radGridView1.DataSource = query.ToList();
}
private
GridViewTemplate CreateChildTemplate()
{
GridViewTemplate childTemplate =
new
GridViewTemplate();
this
.radGridView1.Templates.Add(childTemplate);
GridViewTextBoxColumn column =
new
GridViewTextBoxColumn(
"OrderDate"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"Freight"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"ShipName"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"ShipCountry"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"ShipCity"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"ShipAddress"
);
childTemplate.Columns.Add(column);
childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
return
childTemplate;
}
You can then apply the needed column formatting in the respective section. The result (without any modifications of the above code) is as follows:
The source code of example can be downloaded from here (link). It uses RadControls for WinForms Q3 2010, which you can download here.
Next time we will show you how to use the Load on Demand hierarchy with WCF data service.
Enjoy, and don’t forget to leave your comments!
Nikolay Diyanov Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.
Find him on Twitter @n_diyanov or on LinkedIn.