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

[Solved] Grouping DataTemplate on dynamic grid

3 Answers 172 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Doug
Top achievements
Rank 1
Doug asked on 27 Jul 2011, 02:31 AM
 
 

I'm trying to programmatically set the grouping to 2 columns that use DataTemplates, but I can't get it to work, nor can I get the drag and drop grouping to work. 

I think my problem lies with the DataMemberPath, but I don't know what context it has access to at rntime, so I don't know what string to use for it.

I'm using version 2011.2.712.1040 of the RadGridView bound to an itemsource (PagedCollectionView).  On AutoGeneratingColumn, I'm creating a new GridViewColumn with a DataTemplate as the CellTemplate property.

Here's the xaml for the grid:

 

<telerik:RadGridView telerik:Theming.Theme="Office_Blue" EnableColumnVirtualization="False" EnableRowVirtualization="False" x:Name="dgMetricContainer" ShowGroupPanel="True" Grid.Row="2" GridLinesVisibility="Both"

                          AutoGenerateColumns="True" AutoGeneratingColumn="dgMetricContainer_AutoGeneratingColumn" Margin="8" Grid.ColumnSpan="3" ItemsSource="{Binding MetricContainerClass.CollectionData}" FrozenColumnCount="{Binding FrozenColumnCount}" AutoExpandGroups="True"
            </telerik:RadGridView>

Here's the autogeneratingcolumn event where I try to set the grouping:

private void dgMetricContainer_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            var tempCol = new MyDataGridTemplateColumn();
            tempCol.Header = header.DisplayName;
            tempCol.ColumnName = e.Column.UniqueName;
            tempCol.CellTemplate = (DataTemplate)Resources["metricContainerCellTemplate"];
            tempCol.IsReadOnly = true;
            tempCol.IsGroupable = true;
            tempCol.GroupMemberPath = "???";
            e.Column = tempCol;
            if (vm.RowSpanColumnNames.Contains(e.Column.UniqueName))
            {
                var descriptor = new GroupDescriptor();
                descriptor.Member = "StrategyAlignmentName";
                descriptor.SortDirection = ListSortDirection.Ascending;
                dgMetricContainer.GroupDescriptors.Add(descriptor);
            }
        }

I think I may be missing something fundamental here.  What should my GroupMemberPath be?

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Jul 2011, 08:25 AM
Hello Doug,

Firstly, I would recommend you to take a look at our online documentation for a reference on Grouping. Generally, by default the GroupMemberPath is set to the corresponding property the column is bound to.Setting it to a different property from your business object will signify that once you drag that column, the grouping will be based on the values from another column. Basically, if the default state suits your needs, you do not have to set it at all. 
Furthermore, you do not need to define the GroupDescriptors in the AutoGeneratingColumn event. You may set it during the initialization for example - as it is illustrated in the documentation. 
On a side note, based on the code provided, I get the impression that the DataMemberBinding property of the column is not set. Actually, it needs to be set to the property from your business object that you want to be displayed in this column. You may run through this article for a reference. 
Please let me know in case you need any further assistance or a sample project on it.
 

All the best,
Maya
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Doug
Top achievements
Rank 1
answered on 27 Jul 2011, 01:17 PM
I wanted to post the DataTemplate code that each GridViewColumn (a base class for MyDataGridTemplateColumn below) is using for it's cell template:

 

<DataTemplate x:Key="metricContainerCellTemplate">
            <StackPanel Orientation="Vertical">
                <telerik:ItemsControl x:Name="valueList">
                    <telerik:ItemsControl.ItemTemplate>
                        <DataTemplate>
                     <TextBlock Name="txtMetricValue" Text="{Binding MetricValue,Converter={StaticResource NumberConverter}}" MinHeight="18" MaxWidth="125" TextWrapping="Wrap" TextAlignment="Left" Margin="4" /></DataTemplate>
                    </telerik:ItemsControl.ItemTemplate>
                </telerik:ItemsControl>
            </StackPanel>
        </DataTemplate>

It is more complicated than that, containing more controls, but this gives the idea.  I'm using GridViewColumn instead of GridViewDataColumn and I'm setting the cell template to this data template.  The info I want to group by is the MetricValue that I'm binding the TextBlock's Text property to.

If I set the GridViewColumn's GroupMemberPath to "MetricValue", and then drag the grouping at runtime, it groups everything within 1 blank group.  If I set the GroupMemberPath to anything else, it doesn't attempt to do any grouping at all.  Do you have any more thoughts?  I have looked at the links you sent, but I'm not doing a simple binding, I actually probably have 10 different controls binding visibility, border, text, and other properties in that DataTemplate.

I also tried switching the GridViewColumn base to a GridViewDataColumn base and set the DataMemberBinding
tempCol.DataMemberBinding = new Binding("MetricValue");
but that didn't work either.
0
Maya
Telerik team
answered on 28 Jul 2011, 09:17 AM
Hi Doug,

Grouping will be enabled out-of-the-box if you use a GridViewDataColumn and set the DataMemberBinding or use a GridViewColumn and set the GroupMemberPath. 
Based on the code provided, I have prepared a small project. May you take a look at it and try to reproduce the behavior you reported ? Am I missing something in accomplishing your scenario ? 

Greetings,
Maya
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Doug
Top achievements
Rank 1
Answers by
Maya
Telerik team
Doug
Top achievements
Rank 1
Share this question
or