This example shows the advanced hierarchy model of Telerik RadGrid. You can set the table relations declaratively or programmatically through the GridTableView.ParentTableRelation
property. Each entry in this collection consists of a relation key names. These key names have to be also populated in each GridTableView DataKeyNames
When these properties are specified correctly, Telerik RadGrid will be able to determine the child records of each GridTableView when the control builds the hierarchy, without handling the DetailTableDataBind event.
In order to achieve this you must set the ParentTableRelation
property of every detail table view to indicate how its data table links to the parent table view's data table.
is a collection of GridRelationFields
objects. Each object specifies the link between a single field in the detail table and the corresponding field in the parent table.
It does this using two properties:
- The MasterKeyField property is the name of a field in the data source of the parent table. This property must match exactly a string in the DataKeyNames collection of the parent table view.
- The DetailKeyField property is the name of a field in the data source of the child table whose value must match the parent field specified by MasterKeyField.
If you are using declarative data sources, this string must match exactly the name of a SELECT parameter in the detail table view's data source.
To summarize, you need to define the ParentTableRelations/DataKeyNames
for the MasterTableView/GridTableViews
according to the database relations conventions.
And here are the exact conventions:
- the primary key column name for each table in the grid source (used for master/detail table population) should be added to the DataKeyNames collection of the respective master/detail table;
- the MasterKeyField in the GridRelationFields should match the primary key of the parent table in the corresponding relation;
- the DetailKeyField in the GridRelationFields should match the foreign key of the child table in the corresponding relation.
There is one more detail if you use declarative binding using DataSource
controls under .NET 2.x/3.x:You should have WHERE
clause in the SelectCommand
of the second DataSource
control which to filter the records for the child table. The WHERE
clause should include the DetailKeyField
from the ParentTableRelation
definition between the master/child table.
Furthermore, that same field has to be included in the SelectParameters
of the second DataSource (with exactly the same Name
An important detail is that every GridRelationFields
should have only one field name for DetailKeyField
. For multi-hierarchy relations you can use multiple relation fields as in the example below:
<telerik:GridRelationFields DetailKeyField="ID1" MasterKeyField="ID1" />
<telerik:GridRelationFields DetailKeyField="ID2" MasterKeyField="ID2" />
<telerik:GridRelationFields DetailKeyField="ID3" MasterKeyField="ID3" />
Note that hierarchical grid structure is not supported with simple data-binding (calling DataBind()). See the Simple data binding
demo from the Populating with data
section for more info about the limitations of this binding mode.
You can also check out the different load modes of the grid by changing the setting at the top of the demo