Edit this page

Self-Referencing GridView

Except displaying hierarchical data and applying some custom criteria about when the subset of data to be displayed, RadGridView allows you to define a relation that points back to the same table. In the cases when the hierarchical data is build from one type of items you can use a self referencing RadGridView to display the data.

This tutorial illustrates building a self-referencing hierarchy.

Here is an ordinary RadGridView declaration, populated with some data:

<telerik:RadGridView Margin="8" x:Name="radGridView" AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding EmployeeID}"
                                Header="ID" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"
                                Header="Name" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Title}"
                                Header="Title"
                                UniqueName="Title" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

this.radGridView.ItemsSource = RadGridViewSampleData.GetEmployees();
Me.radGridView.ItemsSource = RadGridViewSampleData.GetEmployees()

This features allows you to display objects which are related to objects of the same type, thus modelling a tree structure. For example, consider an Employee object with a property RelatedTo of type int, which holds the EmployeeID of the related object.

You can build a self-refencing hierarchy by performing the following steps:

  • Define a ChildeTableDefinition for your RadGridView:

<telerik:RadGridView AutoGenerateColumns="False">
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <!--...-->
</telerik:RadGridView>
  • Set the Relation property of GridViewTableDefinition to an instance of the TableRelation class and set its IsSelfReference property to True.

<telerik:RadGridView AutoGenerateColumns="False">
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:TableRelation IsSelfReference="True">
                </telerik:TableRelation>
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <!--...-->
</telerik:RadGridView>
  • Set the FieldNames property of the TableRelation. It is a collection of FieldDescriptorNamePairs.

<telerik:RadGridView AutoGenerateColumns="False">
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:TableRelation IsSelfReference="True">
                    <telerik:TableRelation.FieldNames>
                        <telerik:FieldDescriptorNamePair
                    ParentFieldDescriptorName="EmployeeID"
                    ChildFieldDescriptorName="RelatedTo" />
                    </telerik:TableRelation.FieldNames>
                </telerik:TableRelation>
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <!--...-->
</telerik:RadGridView>

The FiledDescriptorNamePair allows to specify the relations between the objects in the RadGridView's source. It has two important properties:

ParentFieldDescriptorName - specifies the name of the property of the object in the parent RadGridView, that is on the one side of the relation.

ChildFieldDescriptorName - specifies the name of the property of the object in the child RadGridView, that is on the other side of the relation.

In the child RadGridView these entries of the RadGridView's source are displayed, which values of the property, specified as the ChildFieldDescriptorName, are equal to the value of the property, specified as the ParentFieldDescriptorName.

Here is a snapshot of the result:

You can specify more than one FiledDescriptorNamePair. For example, define a relation that specifies that the child objects should have the same country as the parent object.

<telerik:RadGridView AutoGenerateColumns="False">
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:TableRelation IsSelfReference="True">
                    <telerik:TableRelation.FieldNames>
                        <telerik:FieldDescriptorNamePair
                     ParentFieldDescriptorName="EmployeeID"
                     ChildFieldDescriptorName="RelatedTo" />

                        <telerik:FieldDescriptorNamePair
                    ParentFieldDescriptorName="Country"
                    ChildFieldDescriptorName="Country" />
                    </telerik:TableRelation.FieldNames>
                </telerik:TableRelation>
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <!--...-->
</telerik:RadGridView>

In the snapshot below you can see that for the object with EmployeeID equal to 1 only the employee with RelatedTo property equal to 1 and Country equal to Germany is displayed.

See Also