RadGridVew with “Custom Hierarchy Template” and dynamic columns

2 posts, 0 answers
  1. John
    John avatar
    1 posts
    Member since:
    Mar 2013

    Posted 07 Mar 2013 Link to this post


    I am trying to create a RadGridView with a child template, I based my code this example : http://demos.telerik.com/silverlight/#GridView/Hierarchy/HierarchyChildTemplate

    But on top of this I wanted to have a number of dynamic columns at the top level and also at the child template level.  However when I set the XAML AutoGenerateColumns attribute to True it does not interpret the column with the data for the child template, it just processes it as a regular field value.

    I am using the DataTable objects to store the dynamic property values.

    What do I need to do to get WPF to process the "LowerLevel" property into the ChildTemplate?

    (FYI: I am consuming an XML file hence the need for dynamic columns, below is a simplified example of what I am trying to do)

    Thanks for your help!


            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="WpfRadGridViewCustomHierarchyDynamicColumns.MainWindow"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
            <telerik:RadGridView x:Name="DynamicGridUi" ShowColumnFooters="True" GroupRenderMode="Flat"
                        <telerik:RadGridView x:Name="RadGridView1" GroupRenderMode="Flat"
                                             ItemsSource="{Binding LowerLevel}"

    using System;
    using System.Collections.Generic;
    using System.Data;
    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;
    namespace WpfRadGridViewCustomHierarchyDynamicColumns
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
            public MainWindow()
            private void Window_Loaded(object sender, RoutedEventArgs e)
                Random gen = new Random();
                DataTable dtTopLevel = new DataTable();
                // Define the top level columns
                dtTopLevel.Columns.Add("KnownCol", typeof(string));
                // Generate dynamic column names for top level
                List<string> DynamicColumns = new List<string>();
                int intColCount = gen.Next(1, 6);
                for (int i = 0; i < intColCount; i++)
                    DynamicColumns.Add("Col" + i);
                // Add those dynamic columns to the top level
                foreach (string strItem in DynamicColumns)
                    dtTopLevel.Columns.Add(strItem, typeof(string));
                // DataTable for child template
                dtTopLevel.Columns.Add("LowerLevel", typeof(DataTable));
                string[] RowDataForKnownColumn = new string[] { "1st", "2nd", "3rd", "4th", "5th", "6th" };
                foreach (string item in RowDataForKnownColumn)
                    // Add cell value for known column
                    var aryStart = new object[] { item };
                    // Generate dynamic columns cell values
                    List<string> listCellValuesForRow = new List<string>();
                    foreach (string strColumnName in DynamicColumns)
                        listCellValuesForRow.Add("Val" + item + "," + strColumnName);
                    // Data for Child Template
                    DataTable dtLowerLevel = new DataTable();
                    // Define the columns for the lower level data for each row
                    foreach (string strColumnName in DynamicColumns)
                        dtLowerLevel.Columns.Add(strColumnName, typeof(string));
                    // Then generate and add the cell data for the lower level table
                    for (int i = 0; i < gen.Next(1, RowDataForKnownColumn.Length); i++)  // generate a random number of rows for the lower
                        List<string> listLowerLevelCellValues = new List<string>();
                        foreach (string strColumnName in DynamicColumns)
                            // Generate the cell contents for the the row in the lower table
                            listLowerLevelCellValues.Add("LowerVal " + RowDataForKnownColumn[i] + "," + strColumnName);
                        // Add the cell values for the current row in the lower table
                    // End data for child template
                    // Include the cell values for the dynamic columns for this row
                    var aryMiddle = listCellValuesForRow.ToArray();
                    // Include the the data table value which contains the row values for the lower level
                    var aryEnd = new object[] { dtLowerLevel };
                    // Concatenate all the row values into a single array
                    var aryRowValues = aryStart.Concat(aryMiddle).ToArray().Concat(aryEnd).ToArray();
                    // Add all the cell values for this row in the top level
                DynamicGridUi.ItemsSource = dtTopLevel;

  2. Vlad
    Vlad avatar
    11100 posts

    Posted 12 Mar 2013 Link to this post


     You can use the Loaded event of the child grid to access the child grid itself  (sender argument in the event handler). 

    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top