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

Get item values in the GroupHeaderTemplate

12 Answers 352 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edward
Top achievements
Rank 2
Edward asked on 07 Nov 2012, 11:00 AM

Hi

I want to fetch the SalePrice in the label below, it does not work. The below code is what I got so far.

After searching for a few hours I give up.  I hope you can help. 



<telerik:GridViewDataColumn UniqueName="Title" IsReadOnly="True" IsVisible="True"  x:Name="showTitleColumn" DataMemberBinding="{Binding Path=OrderDetailGroupingTitle}" Header="Titel" Width="auto">
 
 
                        <telerik:GridViewDataColumn.GroupHeaderTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
 
                                    <telerik:Label Content="Test" />
                                    <telerik:Label Foreground="{StaticResource StrongBrush}" Content="{Binding Group.Items[0].SalePrice}"  />
                                    <telerik:Label Foreground="{StaticResource StrongBrush}" FontWeight="Bold" Content="{Binding Group.Key}"  />
 
                                </StackPanel>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.GroupHeaderTemplate>
 
                    </telerik:GridViewDataColumn>

12 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 07 Nov 2012, 04:40 PM
Hello,

The DataContext of the group row (and the StackPanel consequently) is GroupViewModel. As you could check in the link, it contains properties like Group, Column, AggregateResults etc. Each Group in the GroupViewModel has an Items collection. Would you please check if the Items[0] object is present and has a property SalePrice?

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 08 Nov 2012, 08:55 PM
Hi

I don't know how to to check how Items[0] object is present and has a property SalePrice.  I cannot break an inspect in the xaml there.

I understand that the datacontext is not the same one as the grid itself.  Maybe I should go that route and set the context to the one of the grid and bind normally?  I searched for examples but they did not work either.

Do you have a sample where you group in a column template and bind one of the regular controls to a grid collection item ? 

Thanks anyway


0
Dimitrina
Telerik team
answered on 09 Nov 2012, 09:36 AM
Hello,

I have attached a sample project showing how you could bind to the property of the Items[0] item. As I previously said the DataContext of the group row is a GroupViewModel. Do you need it to be something else?

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 21 Nov 2012, 04:39 PM
Hi

Thanks for the example. It is almost what I want.

This example works with only one level of grouping but does not work for two. Let me show you:

<telerik:RadGridView Grid.Row="0"
                          Name="clubsGrid"
                          ItemsSource="{Binding Clubs}"
                          AutoGenerateColumns="False">
          
         <telerik:RadGridView.GroupDescriptors>

             <telerik:GroupDescriptor Member="Name"/>

         </telerik:RadGridView.GroupDescriptors>
          
         <telerik:RadGridView.Columns>
             <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
                 <telerik:GridViewDataColumn.GroupHeaderTemplate>
                     <DataTemplate>
                         <StackPanel>
                             <TextBlock Foreground="#FFED1691"
                                    Text="{Binding Group.Items[0].StadiumCapacity}" />
                             <TextBlock Foreground="#FFED7971"
                                    Text="{Binding Group.Key}" />
                         </StackPanel>
                     </DataTemplate>
                 </telerik:GridViewDataColumn.GroupHeaderTemplate>
 
             </telerik:GridViewDataColumn>
          
             <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}" DataFormatString="yyyy" Header="Est." />
              
         </telerik:RadGridView.Columns>
          
          
     </telerik:RadGridView>


<telerik:RadGridView Grid.Row="0"
                          Name="clubsGrid"
                          ItemsSource="{Binding Clubs}"
                          AutoGenerateColumns="False">
          
         <telerik:RadGridView.GroupDescriptors>

             
  <telerik:GroupDescriptor Member="Name"/>
             <telerik:GroupDescriptor Member="Established"/>


         </telerik:RadGridView.GroupDescriptors>
          
         <telerik:RadGridView.Columns>
             <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
                 <telerik:GridViewDataColumn.GroupHeaderTemplate>
                     <DataTemplate>
                         <StackPanel>
                             <TextBlock Foreground="#FFED1691"
                                    Text="{Binding Group.Items[0].StadiumCapacity}" />
                             <TextBlock Foreground="#FFED7971"
                                    Text="{Binding Group.Key}" />
                         </StackPanel>
                     </DataTemplate>
                 </telerik:GridViewDataColumn.GroupHeaderTemplate>
 
             </telerik:GridViewDataColumn>
          
             <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}" DataFormatString="yyyy" Header="Est." />
              
         </telerik:RadGridView.Columns>
          
          
     </telerik:RadGridView>


if you would copy and paste this code in the project the wysiwyg VS editor will show the difference.


I hope you can help!

Thanks,
Edward


0
Dimitrina
Telerik team
answered on 23 Nov 2012, 05:09 PM
Hello,

Indeed, you are right. Please predefine the HeaderTemplate for the column bound to the "Established" property and let me know if now the text in the GroupHeaderTemplate is the one you expect? 

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 26 Nov 2012, 02:48 PM
Unfortunately its still empty. I did:


<telerik:RadGridView Grid.Row="0"
                           Name="clubsGrid"
                           ItemsSource="{Binding Clubs}"
                           AutoGenerateColumns="False">
           
          <telerik:RadGridView.GroupDescriptors>
              <telerik:GroupDescriptor Member="Name"/>
              <telerik:GroupDescriptor Member="Established"/>
          </telerik:RadGridView.GroupDescriptors>
           
          <telerik:RadGridView.Columns>
              <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
                  <telerik:GridViewDataColumn.GroupHeaderTemplate>
                      <DataTemplate>
                          <StackPanel>
                              <TextBlock Foreground="#FFED1691"
                                     Text="{Binding GroupDescriptor.Established}" />
                              <TextBlock Foreground="#FFED7971"
                                     Text="{Binding Group.Key}" />
                          </StackPanel>
                      </DataTemplate>
                  </telerik:GridViewDataColumn.GroupHeaderTemplate>
 
              </telerik:GridViewDataColumn>
           
              <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}" DataFormatString="yyyy" Header="Est." />
               
          </telerik:RadGridView.Columns>
           
           
      </telerik:RadGridView>




0
Dimitrina
Telerik team
answered on 27 Nov 2012, 08:51 AM
Hello,

The values does not shown as this Binding is not correct. 

<TextBlock Foreground="#FFED1691" Text="{Binding GroupDescriptor.Established}" />

Please note that the DataContext of the group row (and the StackPanel consequently) is GroupViewModel. As you can check in the link, it contains properties like Group, Column, AggregateResults, Items etc. Each Group in the GroupViewModel has an Items collection. Would you please check if the Items[0] object is present for the specific group rows and has the property which value you would like to show?

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 27 Nov 2012, 12:57 PM
Sorry, my mistake:

I ment:

<TextBlock Foreground="#FFED1691"   Text="{Binding Group.Items[0].Established}" />

Still nada.

It looks like the Items[0] object is present (not sure how to check this) but not the property.  How do I get access to that Established property ?

Greetings,

Edward




0
Dimitrina
Telerik team
answered on 27 Nov 2012, 01:33 PM
Hi,

When you have more than 1 grouping, then the most inner Group Row will have Items of type the bound business object. That is why it shows the value of the Established property. When we talk about the outer Group Rows, then their Items are the inner Group Rows and they do not have an Established property.

Please find my test project attached for you reference.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 27 Nov 2012, 02:25 PM
Thanks for explaining. The attached project has the same result as the project we first started with :(

So how can I access another data item of the business object in the outer Group rows?  Via InnerGroup ? 

The result should look Like:

Liverpool 1892
-- whatever
Manchester United 1878
-- whatever
...
..
.


Greeting,
Edward
0
Accepted
Dimitrina
Telerik team
answered on 27 Nov 2012, 02:32 PM
Hi,

In that case you will have to define the HeaderTemplate for the "Name" column like so:

<telerik:GridViewDataColumn.GroupHeaderTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Foreground="Green"
                 Text="Test Items[0].Established" />
            <TextBlock Foreground="Green"
                 Text="{Binding Group.Items[0].Items[0].Established}" />
            <TextBlock Foreground="#FFED7971"
                 Text="Test Key" />
            <TextBlock Foreground="#FFED7971"
                 Text="{Binding Group.Key}" />
        </StackPanel>
    </DataTemplate>
 </telerik:GridViewDataColumn.GroupHeaderTemplate>

Please note that you this will work when you have grouping on two columns (Name and Established in your case).


Kind regards, Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward
Top achievements
Rank 2
answered on 27 Nov 2012, 03:16 PM
That worked !

Thanks!
Tags
GridView
Asked by
Edward
Top achievements
Rank 2
Answers by
Dimitrina
Telerik team
Edward
Top achievements
Rank 2
Share this question
or