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

How to define columns, properties for all-level hierarchical child tables

5 Answers 347 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 1
Sergey asked on 13 Jul 2019, 03:09 AM

Hi,

Could you please help me to figure out how to define columns and properties for all-level nested tables in hierarchical gridview?

I'm referring to pages:

https://docs.telerik.com/devtools/wpf/controls/radgridview/hierarchical-gridview/basic-hierarchies

https://docs.telerik.com/devtools/wpf/controls/radgridview/hierarchical-gridview/how-to/access-child-gridview

 

But columns and properties AutoGenerateColumns and ShowGroupPanel are applied only for first child table and second-level table and deeper is not affected by that property.

MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HierarchyTest"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="HierarchyTest.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="513.789" Width="1084.149">
    <Grid>

        <telerik:RadGridView ItemsSource="{Binding}" DataLoading="RadGridView_DataLoading" AutoGenerateColumns="False" telerik:StyleManager.Theme="Fluent">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Id}" Header="Id" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding StartTime}" Header="Start Time" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}" Header="Type" />
            </telerik:RadGridView.Columns>
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition />
            </telerik:RadGridView.ChildTableDefinitions>
            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <telerik:RadGridView ItemsSource="{Binding ChildJobs}" Name="childGrid" ShowGroupPanel="False" telerik:StyleManager.Theme="Fluent" AutoGenerateColumns="False">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Id}" Header="Id" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding StartTime}" Header="Start Time" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}" Header="Type" />
                        </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
        </telerik:RadGridView>
    </Grid>
</Window>

 

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;

namespace HierarchyTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<Build> Builds { get; set; } = new ObservableCollection<Build>();

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this.Builds;

            this.Builds.Add(new Build() { Id = 1 });

            foreach (int i in Enumerable.Range(0, 10))
            {
                this.Builds[0].ChildJobs.Add(new Job() { Id = i });
                this.Builds[0].ChildJobs[i].ChildJobs.Add(new Job() { Id = i + 1 });
            }
        }

        private void RadGridView_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)
        {
            var dataControl = (GridViewDataControl)sender;

            if (dataControl.ParentRow != null && dataControl.ChildTableDefinitions.Count == 0)
            {
                dataControl.ChildTableDefinitions.Add(new GridViewTableDefinition() { Relation = new PropertyRelation("ChildJobs") });
            }
        }
    }

    public class Build
    {
        public int Id { get; set; }

        public DateTime StartTime { get; set; } = DateTime.Now;

        public string Type { get; set; } = "Build";

        public ObservableCollection<Job> ChildJobs { get; set; } = new ObservableCollection<Job>();
    }

    public class Job
    {
        public int Id { get; set; }

        public DateTime StartTime { get; set; } = DateTime.Now;

        public string Type { get; set; } = "Job";

        public ObservableCollection<Job> ChildJobs { get; set; } = new ObservableCollection<Job>();
    }
}

 

5 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 17 Jul 2019, 01:16 PM
Hello Sergey,

I've attached a small example showing how to show multi-level hierarchy, based on your model. Can you please give it a try and tell me if it it helps?

Regards,
Martin Ivanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sergey
Top achievements
Rank 1
answered on 21 Jul 2019, 12:07 AM

Hi,

Your example works great! Thank you!

0
Sergey
Top achievements
Rank 1
answered on 21 Jul 2019, 11:22 PM
Small question: how to avoid empty table if there are no elements in ChildJobs property or it's equal null? 
0
Accepted
Martin Ivanov
Telerik team
answered on 23 Jul 2019, 08:21 AM

Hello Sergey,

To avoid empty table you can bind the IsExpandable property of the rows (using the IsExpandable of RadGridView) to the ChildJobs collection's count. I've updated my last project to show this approach. I hope it helps.

Regards, Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
deri
Top achievements
Rank 1
commented on 17 Jan 2024, 10:43 AM | edited

Hi Telerik Teams,

I know it's been long time, but how can i get this behavior using MVVM, and for technical reason i don't want to pass any View to my ViewModel. I have a Collection<Track> and each of it has a child collection of the same type (Track), and so on. The tree depth may be vary depends on the data collected from the network. From your attached example, i can only view to the second node, and that's all. If i want to view deeper node, i should add more HierarchyChildTemplate in the View side, and i don't know why is the last node table displays all the property of the Track object, even i set the AutoGenerateColumns to False. Any further guidance would be great.

Thanks in advance.

0
Sergey
Top achievements
Rank 1
answered on 26 Jul 2019, 10:50 PM
Martin, thank you for help!
Tags
GridView
Asked by
Sergey
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Sergey
Top achievements
Rank 1
Share this question
or