Loading hierarchy dynamic data

3 posts, 0 answers
  1. Shawn
    Shawn avatar
    13 posts
    Member since:
    Sep 2010

    Posted 05 Dec 2012 Link to this post

    Trying to create a grid view similar to the attached image where I need to display result of web service call that returns datamap hierarchy in form of array of name/value pair. To display this hierarchy, each DataMap element should display as a row and when one or more of the items in that row is another DataMap it’ll be display as child row. So far I was able to load the top elements by loading rows to the collection of dynamic objects and assigning it to GridView ItemsSource. As you see I subscribe the child grid view “Loaded” event so I can load the children when row is expanded by the user. The problem I am having is sender object (child grid) TableDefinition is null when the event is sent so I have no way of knowing which DataMap should be loaded in child GridView. How can I access Table Definition of the GridView being loaded? Is this correct way of achieving desired output or can you recommend easier way of doing this?

    Thanks,

    Shawn

    Here is my data classes:
    public class DataMap
    {
        public DataEntry[] DataEntries { get; set; }
    }
    public class DataEntry
    {
       public string Name { get; set; }
        public object Item { get; set; }
    }
     
    Here is XAML:
    <Controls:RadGridView x:Name="gridView"
                          RowIndicatorVisibility="Visible"
                          AutoGenerateColumns="False"
                          AlternateRowBackground="#FF36E84F"
                          ShowGroupPanel="False"
                          IsReadOnly="True"
                          CanUserDeleteRows="False"
                          CanUserInsertRows="False"
                          EnableColumnVirtualization="False"
                          IsFilteringAllowed="False">
        <Controls:RadGridView.HierarchyChildTemplate>
            <DataTemplate x:Name="childGridView">
                <Controls:RadGridView RowIndicatorVisibility="Visible"
                          AutoGenerateColumns="False"
                          AlternateRowBackground="#FF36E84F"
                          ShowGroupPanel="False"
                          IsReadOnly="True"
                          CanUserDeleteRows="False"
                          CanUserInsertRows="False"
                          EnableColumnVirtualization="False"
                          IsFilteringAllowed="False" Loaded="RadGridView_Loaded"/>
            </DataTemplate>
        </Controls:RadGridView.HierarchyChildTemplate>
    </Controls:RadGridView>
     
    Here is Code behind to load the grid:
    private void loadList(RadGridView grid, DataMap[] data)
       {
          var list = new ObservableCollection<DataRow>();
            foreach (var dataMap in data)
            {
                var dataRow = new DataRow();
                foreach (var dataEntry in dataMap.DataEntries)
                {
                    dataRow.AddColumn(dataEntry.Name, dataEntry.Item);
                    var column = new GridViewDataColumn
                    {
                        Header = dataEntry.Name,
                        DataMemberBinding = new Binding(dataEntry.Name)
                    };
                    var itemType = dataEntry.Item.GetType();
                    if (itemType == typeof(DataMap[]))
                    {
                       var temp = Resources["imageCellTemplate"];
                       column.CellTemplate = (DataTemplate)Resources["imageCellTemplate"];
                       grid.ChildTableDefinitions.Add(new GridViewTableDefinition{Relation = new PropertyRelation(dataEntry.Name), DataSource = dataEntry.Item});
     
                    }
                 grid.Columns.Add(column);
                 }
                   list.Add(dataRow);
            }
            grid.ItemsSource = list;
        }
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 06 Dec 2012 Link to this post

    Hello,

     You can cast your child grid DataContext to your item type and access desired property value. 

    Regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Shawn
    Shawn avatar
    13 posts
    Member since:
    Sep 2010

    Posted 06 Dec 2012 Link to this post

    Thanks Vlad DataContext value contains Parent Row data. How can I get the DataSource associated with the TableDefinition or Relation value? These values are null.
Back to Top