This example shows the self-referencing hierarchy model of Telerik RadGrid. It allows you to build multiple levels of hierarchy from a single table in the data source by specifying relations inside the same table. You do not need to create separate tables for each child level - one table is sufficient to define the hierarchy schema. All visual settings will be applied to the nested tables as well.
Below are steps needed to implement the self-referencing feature of the grid:
- add the fields from the grid source which define the parent/child relations (EmployeeID and ReportsTo in this case) to the DataKeyNames array of the MasterTableView
- configure these fields through the SelfReferencingSettings property of the MasterTableView. The ParentKeyName subproperty has to point to the parent item id, while KeyName references the id value for the current table level.
- define the root level filter for the grid hierarchy depending on your source table conventions (in this demo the root items have a parent ID with null
value). This can be done through the FilterExpression property of the MasterTableView instance:
RadGrid1.MasterTableView.FilterExpression = "ReportsTo IS NULL"
- you can also set the MaximumDepth sub-property of SelfReferencingSettings
(not used in this example) to limit the depth of the nested tables. The default value for
MaximumDepth is 10. This property can be set only once when the grid is initialized and can not
be modified
In addition, you can hide the header item for the tables in the hierarchy wiring the ItemCreated event of the grid if it is not needed.
Further information about the self-referencing feature of Telerik RadGrid you can find below:
Self-referencing hierarchy