How to apply Converter to GridView footer with aggregate function.

7 posts, 1 answers
  1. Dipti
    Dipti avatar
    37 posts
    Member since:
    Dec 2011

    Posted 05 Feb 2012 Link to this post

    Hi,
    I am using a telerik RadGrid. I display double values in the grid. And the footer column has the Grand Total of the double values.

    I have applied a NegativeNumberConverter to all the double values so that if a value is negative, then it is diplayed with brackets aound it and is comma separated.

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {        Double doubleValue = Double.Parse(value.ToString());
            if (doubleValue < 0)
                return ("(-)" + Math.Abs(doubleValue)).ToString();
            if (double.TryParse(value.ToString(), out doubleValue))
            return doubleValue.ToString("N");
    }

    Also if the value is negative, it is displayed in red color using StyleSelector.

          public class TotalStyleSelector : StyleSelector
        {
            public override Style SelectStyle(object item, DependencyObject container)
            {
                var style = new Style(typeof(GridViewCell));
                var cell = (GridViewCell)container;
                var obj = (InventoryAnalysisEntity)item;
                  if (obj.Total < 0)
                {
                    style.Setters.Add(new Setter(GridViewCell.ForegroundProperty, new SolidColorBrush(Colors.Red)));
                }
                return style;
            }
        }

    I want to apply similar Converter and formatting to my footer row as well.
    The XAML is follows:

    <telerik:GridViewDataColumn Header="0-30 D" CellStyleSelector="{StaticResource d0_30StyleSelector}"
    DataMemberBinding="{Binding Path=D0_30, Converter={StaticResource NegativeNumberConverter}}" IsFilterable="False" TextAlignment="Right" HeaderTextAlignment="Center" HeaderCellStyle="{StaticResource IntranetGridViewHeaderCellStyle}" FooterCellStyle="{StaticResource IntranetGridViewFooterCellStyle}" >
                        <telerik:GridViewDataColumn.AggregateFunctions>
                            <telerik:SumFunction ResultFormatString="{}{0:N}" />
                        </telerik:GridViewDataColumn.AggregateFunctions>
                        <telerik:GridViewColumn.CellTemplateSelector>
                            <local:D0_30Template>
                                <local:D0_30Template.EmptyDataTemplate>
                                    <DataTemplate>
                                        <TextBlock />
                                    </DataTemplate>
                                </local:D0_30Template.EmptyDataTemplate>
                            </local:D0_30Template>
                        </telerik:GridViewColumn.CellTemplateSelector>
                    </telerik:GridViewDataColumn>


    Please suggest how to do this.

    Thanks.

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Feb 2012 Link to this post

    Hi Dipti,

    Please refer to our demos for a reference. Once you define your column footer, you can use a converter in teh binding similar to the way you have already done.
    Let me know in case you need any further assistance. 

    Kind regards,
    Maya
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Dipti
    Dipti avatar
    37 posts
    Member since:
    Dec 2011

    Posted 06 Feb 2012 Link to this post

    I saw the demo u mentioned, it has formatting, but not Converter. Please tell me how to use Converter in my scenario?
    I tried this but it didn't work.

                  <telerik:GridViewDataColumn Header="% to TM" CellStyleSelector="{StaticResource  percentageToTMStyleSelector}"
    DataMemberBinding="{Binding Path=PercentageToTM, Converter={StaticResource NegativeNumberConverter}}" 
              IsFilterable="False" TextAlignment="Right" HeaderTextAlignment="Center" HeaderCellStyle="{StaticResource IntranetGridViewHeaderCellStyle}" >
                <telerik:GridViewColumn.AggregateFunctions>
                    <telerik:SumFunction />
                </telerik:GridViewColumn.AggregateFunctions>
                <telerik:GridViewDataColumn.Footer>
                    <telerik:AggregateResultsList ItemsSource="{Binding Path=.,Converter={StaticResource NegativeNumberConverter}}" VerticalAlignment="Center">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock VerticalAlignment="Center" Text="{Binding FormattedValue}" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </telerik:AggregateResultsList>
                </telerik:GridViewDataColumn.Footer>
                <telerik:GridViewColumn.CellTemplateSelector>
                    <local:PercentageToTMTemplate>
                        <local:PercentageToTMTemplate.EmptyDataTemplate>
                            <DataTemplate>
                                <TextBlock />
                            </DataTemplate>
                        </local:PercentageToTMTemplate.EmptyDataTemplate>
                    </local:PercentageToTMTemplate>
                </telerik:GridViewColumn.CellTemplateSelector>
            </telerik:GridViewDataColumn>
  4. Dipti
    Dipti avatar
    37 posts
    Member since:
    Dec 2011

    Posted 06 Feb 2012 Link to this post

    Hi Maya,

    I found the solution.
    <telerik:GridViewDataColumn Header="% to TM" CellStyleSelector="{StaticResource percentageToTMStyleSelector}" DataMemberBinding="{Binding Path=PercentageToTM, Converter={StaticResource NegativeNumberConverter}}" IsFilterable="False" TextAlignment="Right" HeaderTextAlignment="Center" HeaderCellStyle="{StaticResource IntranetGridViewHeaderCellStyle}" >
                        <telerik:GridViewColumn.AggregateFunctions>
                            <telerik:SumFunction />
                        </telerik:GridViewColumn.AggregateFunctions>
                        <telerik:GridViewDataColumn.Footer>
                            <StackPanel Background="Transparent">
                                <telerik:AggregateResultsList ItemsSource="{Binding}" VerticalAlignment="Center">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                                <TextBlock VerticalAlignment="Center" Text="{Binding FormattedValue,Converter={StaticResource NegativeNumberConverter}}" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </telerik:AggregateResultsList>
                            </StackPanel>
                        </telerik:GridViewDataColumn.Footer>
                        <telerik:GridViewColumn.CellTemplateSelector>
                            <local:PercentageToTMTemplate>
                                <local:PercentageToTMTemplate.EmptyDataTemplate>
                                    <DataTemplate>
                                        <TextBlock />
                                    </DataTemplate>
                                </local:PercentageToTMTemplate.EmptyDataTemplate>
                            </local:PercentageToTMTemplate>
                        </telerik:GridViewColumn.CellTemplateSelector>
                    </telerik:GridViewDataColumn>

  5. Dipti
    Dipti avatar
    37 posts
    Member since:
    Dec 2011

    Posted 08 Feb 2012 Link to this post

    Hi Maya,
    I could apply Converter and formatting using the method demonstrated in above post.
    I want to align the text in footer to right, i have tried many things, nothing helps.

    <telerik:GridViewDataColumn.Footer>
      <StackPanel Background="Transparent" HorizontalAlignment="Right">
        <telerik:AggregateResultsList ItemsSource="{Binding}" VerticalAlignment="Center">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" >
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding FormattedValue,Converter={StaticResource NegativeNumberConverter}}" />
              </StackPanel>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </telerik:AggregateResultsList>
      </StackPanel>
    </telerik:GridViewDataColumn.Footer>


    Please suggest.
  6. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 08 Feb 2012 Link to this post

    Hi Dipti,

    You could try setting FooterTextAlignment property of the column to 'Right'. 

    Kind regards,
    Maya
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Dipti
    Dipti avatar
    37 posts
    Member since:
    Dec 2011

    Posted 08 Feb 2012 Link to this post

    Thanks Maya, that worked.

    Regards,
    Dipti
Back to Top