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

Custom footer with aggregate function!

11 Answers 844 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Perlom
Top achievements
Rank 1
Perlom asked on 11 Sep 2011, 04:06 PM

Hi,

 

I want to have a footer in one of my gridview columns that has two rows: one containing a textbox bound to a field in model view, and the second row displays the sum of items in that column.

 

---------------------------------

Total:  {text box}

                <sum>

---------------------------------

 

I have followed the suggestion from another thread to have a style for the footer to set the binding, and then reference that style on the column. 

 

<Style x:Key="SalesTotalStyle" TargetType="telerik:GridViewFooterCell">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Width="75" TextAlignment="Right" FontWeight="Bold"  Text="{Binding Path=DataContext.SalesSum,
                               RelativeSource={RelativeSource AncestorType=telerik:GridView}}" />
                    <TextBox Grid.Row="1" Text="{Binding Path=DataContext.ExpetedSalesTotal,                               
                       RelativeSource={RelativeSource AncestorType=telerik:GridView}}" Width="75" TextAlignment="Right"/>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>


This works great and it does display y the data properly in the textbox.  However, how  can I add an aggregate function in the style section?? 
I undetstand aggregate functions will not show if the footer is set.  I have also tried to create a sum property (SalesSum) that reruns the sum of underlying data, whoever it doesn’t get refreshed in the grid as the user enters items in the column.  Any suggestion?

 



Cheers

11 Answers, 1 is accepted

Sort by
0
Perlom
Top achievements
Rank 1
answered on 12 Sep 2011, 05:03 PM

I have also tried modifying the Binding attributes on the DataColumn to Set NotifyOnTargetUpdate and NotifyOnSourceUpdate to true, and then set an event handler for the TargetUpdated and Source Updated in my code behind.  These events are not firing as well.  Are they supported by the DataColumn?

DataMemberBinding="{Binding Path=Quantity, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"




0
Pavel Pavlov
Telerik team
answered on 16 Sep 2011, 09:44 AM
Hi Perlom,

The following XAML demonstrates how to place some custom content in the footer and preserve the AggregateResults  as well.
<telerik:RadGridView x:Name="RadGridView1" ShowColumnFooters="True" AutoGenerateColumns="False" ItemsSource="{Binding Items}" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Count}" >
                    <telerik:GridViewDataColumn.AggregateFunctions>
                        <telerik:SumFunction Caption="Sum:" />
                    </telerik:GridViewDataColumn.AggregateFunctions>
                    <telerik:GridViewDataColumn.Footer>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Grid.Row="1" Text="{Binding Path=DataContext.ExpetedSalesTotal,                               
                                  RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}" Width="75" TextAlignment="Right"/>
                            <telerik:AggregateResultsList ItemsSource="{Binding}"  />
                        </StackPanel>
                    </telerik:GridViewDataColumn.Footer>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

To see this in action , just run the attached sample project.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Perlom
Top achievements
Rank 1
answered on 16 Sep 2011, 06:30 PM
Thanks Pavel, this was helpful :)


0
Eliot
Top achievements
Rank 1
answered on 30 Jan 2015, 04:39 PM
Is there any way to get the stackpanel to render when exporting to Excel?

thanks.

Eliot
0
Eliot
Top achievements
Rank 1
answered on 30 Jan 2015, 04:47 PM
Never mind.

I subclassed the StackPanel and overrode ToString()
0
Stefan
Telerik team
answered on 02 Feb 2015, 12:31 PM
Hello Eliot,

We are glad you have resolution to your issue and shared it with our community.

Do not hesitate to contact us if you have any questions on our controls.

Best Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eliot
Top achievements
Rank 1
answered on 09 Feb 2015, 10:21 AM
Hi Stefan,

I still have an issue with this.

The footer is rendered for GridViewDataColumns  e.g.

 this.radGridView.AutoGenerateColumns = false;
            var columns = this.radGridView.Columns;

            columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("CCY"), Header = "CCY", UniqueName = "CCY", IsReadOnly = true });

However when the column is as GridViewExpressionColumn the footer is not rendered.

 foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
            {

var dateColumn = new GridViewExpressionColumn() { TextAlignment = TextAlignment.Right, Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:N0}", IsReadOnly = true };

 columns.Add(dateColumn);

                Expression<Func<FUNDING_TRADES, decimal?>> consideration = row => 10M;  //Hardcoded 10 for example
                GridViewExpressionColumn considerationExpression = this.radGridView.Columns[day.ToString("dd MMM yyyy")] as GridViewExpressionColumn;
                considerationExpression.Expression = consideration;
}

It doesn't matter what's in the footer itself, the ToString() method of the stackpanel is never called for GridViewExpressionColumns 

Any ideas on this?

thank,

Eliot




0
Stefan
Telerik team
answered on 10 Feb 2015, 05:10 PM
Hello Elliot,

Exporting the footer of a GridViewExpressionColumn is currently not supported out of the box. As a workaround, I can suggest you subscribing for the ElementExporting event and manually setting the exported value of the footer as in the example below:
void clubsGrid_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
    if (e.Context is GridViewExpressionColumn)
    {
        if (e.Element == ExportElement.FooterCell && e.Value == null)
        {
            e.Value = (e.Context as GridViewExpressionColumn)
                .Footer.ToString();
        }
    }
}

Additionally, I believe you might find the exporting FAQ documentation article useful on that matter.

Best Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eliot
Top achievements
Rank 1
answered on 11 Feb 2015, 01:39 PM
Hi Stefan,

That worked perfectly!  thanks

The only issue I have now is getting the figures to line up in Excel.

The footer is over several rows.  This works fine with the stackpanel but there is nothing in Excel to tell it to respect the lines.
I have added \r\n after each child in my ToString method but Excel ignores it.

I realise this is most likely an Excel issue but wanted to see if you have come across this issue before.

I assume it is not possible to have more than one footer row and therefore not possible for the export function to render the footer over more than one row??

thanks for all your help,

Eliot
0
Dimitrina
Telerik team
answered on 12 Feb 2015, 02:20 PM
Hi Eliot,

As it turns out RadGridView's built-in export does not support multiline values export. I can suggest you try exporting your data as illustrated on the SpreadProcessing Integration demo. That way the Environment.NewLine ("\r\n") will be opened fine in Excel.

Furthermore, we are currently working on improving the export experience to allow direct exporting to xlsx and pdf formats. It is going to be introduced with the upcoming Q1 2014.

You can also follow the RadGridView: Export Content as *.xlsx feature request in the feedback portal.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eliot
Top achievements
Rank 1
answered on 20 Feb 2015, 09:22 AM
Hi Dimitrina,

I ended up saving it in CSV format.
This preserved the line breaks.

thanks for your help,

Eliot
Tags
GridView
Asked by
Perlom
Top achievements
Rank 1
Answers by
Perlom
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Eliot
Top achievements
Rank 1
Stefan
Telerik team
Dimitrina
Telerik team
Share this question
or