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

How to apply Converter to GridView footer with aggregate function.

6 Answers 258 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dipti
Top achievements
Rank 1
Dipti asked on 06 Feb 2012, 05:53 AM
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.

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 06 Feb 2012, 07:34 AM
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 >>
0
Dipti
Top achievements
Rank 1
answered on 06 Feb 2012, 10:19 AM
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>
0
Dipti
Top achievements
Rank 1
answered on 06 Feb 2012, 10:45 AM
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>

0
Dipti
Top achievements
Rank 1
answered on 08 Feb 2012, 06:40 AM
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.
0
Accepted
Maya
Telerik team
answered on 08 Feb 2012, 07:44 AM
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 >>
0
Dipti
Top achievements
Rank 1
answered on 08 Feb 2012, 08:30 AM
Thanks Maya, that worked.

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