This is a migrated thread and some comments may be shown as answers.

How to define HierarchyChildTemplate with Self Reference ChildTableDefinitions

2 Answers 62 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pushpendra
Top achievements
Rank 1
Pushpendra asked on 13 Feb 2013, 02:54 PM
Hi,

My collection contains ID and ParentID fields, I have to show hierarchal grid based on self reference of ID and ParentID with selected columns for parent and child grids. The same I have achieved from code behind using RadGridView.ChildTableDefinitions and GridView_DataLoading event, but now instead of generating child grid column at run time in dataloading event I want to fix these columns in xaml itself. I have tried many ways but HierarchyChildTemplate is not showing any data since I am unable to define its data source. Please find the following code:
 XAML Code:
<radGrid:RadGridView ItemsSource="{Binding EmployeeList}" AllowDrop="True" IsReadOnly="False" 
                       Style="{StaticResource CommonGridStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
    <radGrid:RadGridView.Columns>
        <radGrid:GridViewDataColumn Header="ID" DataMemberBinding="{Binding ID}" IsReadOnly="True" />
        <radGrid:GridViewDataColumn Header="Role" DataMemberBinding="{Binding Role}" IsReadOnly="True" />
        <radGrid:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" IsReadOnly="True" />
        <radGrid:GridViewDataColumn Header="Parent ID" DataMemberBinding="{Binding ParentID}" IsReadOnly="True" />
    </radGrid:RadGridView.Columns>
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <telerik:TableRelation IsSelfReference="True">
                    <telerik:TableRelation.FieldNames>
                        <telerik:FieldDescriptorNamePair ParentFieldDescriptorName="ID" ChildFieldDescriptorName="ParentID" />
                    </telerik:TableRelation.FieldNames>
                </telerik:TableRelation>
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
    <telerik:RadGridView.HierarchyChildTemplate>
        <DataTemplate>
            <telerik:RadGridView Name="childGrid" ShowGroupPanel="False" >
                <radGrid:RadGridView.Columns>
                    <radGrid:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" IsReadOnly="True" />
                    <radGrid:GridViewDataColumn Header="Role" DataMemberBinding="{Binding Role}" IsReadOnly="True" />
                </radGrid:RadGridView.Columns>
            </telerik:RadGridView>
        </DataTemplate>
    </telerik:RadGridView.HierarchyChildTemplate>
</radGrid:RadGridView>

ViewModel Code:
public class MainPageViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Employee> employeeList;
    public ObservableCollection<Employee> EmployeeList
    {
        get { return employeeList; }
        set
        {
            employeeList = value;
            OnPropertyChanged("EmployeeList");
        }
    }
    public MainPageViewModel(MainPage view)
    {
        view.DataContext = this;
        GetEmployeeList();
    }
    private void GetEmployeeList()
    {
        EmployeeList = new ObservableCollection<Employee>();
        EmployeeList.Add(new Employee() { ID = 1, Role = "Supervisor", Name = "S 1", ParentID = 0 });
        EmployeeList.Add(new Employee() { ID = 2, Role = "Worker", Name = "W 1", ParentID = 1 });
        EmployeeList.Add(new Employee() { ID = 3, Role = "Supervisor", Name = "S 2", ParentID = 0 });
        EmployeeList.Add(new Employee() { ID = 4, Role = "Worker", Name = "W 2", ParentID = 3 });
        EmployeeList.Add(new Employee() { ID = 5, Role = "Worker", Name = "W 3", ParentID = 3 });
        EmployeeList.Add(new Employee() { ID = 6, Role = "Supervisor", Name = "S 3", ParentID = 0 });
    }
    #region Property Change
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler pceh = PropertyChanged;
        if (pceh != null)
        {
            pceh(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion Property Change
}
public class Employee
{
    public int ID { get; set; }
    public string Role { get; set; }
    public string Name { get; set; }
    public int ParentID { get; set; }
}

Please let me know how I can achieve this functionality.

Thanks,
Pushpendra

2 Answers, 1 is accepted

Sort by
0
Nurlan
Top achievements
Rank 1
answered on 08 Aug 2013, 05:30 AM
Did you get a solution?
0
Pushpendra
Top achievements
Rank 1
answered on 26 Nov 2013, 12:57 PM

Hi Nurlan,

Sorry for the delay, but I have not found any solution for this.

Pushpendra

Tags
GridView
Asked by
Pushpendra
Top achievements
Rank 1
Answers by
Nurlan
Top achievements
Rank 1
Pushpendra
Top achievements
Rank 1
Share this question
or