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

Loading hierarchy dynamic data

2 Answers 77 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 05 Dec 2012, 08:19 PM

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 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 06 Dec 2012, 07:35 AM
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.

0
Shawn
Top achievements
Rank 1
answered on 06 Dec 2012, 02:55 PM
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.
Tags
GridView
Asked by
Shawn
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Shawn
Top achievements
Rank 1
Share this question
or