Custom footer with aggregate function!

12 posts, 0 answers
  1. Perlom
    Perlom avatar
    33 posts
    Member since:
    Aug 2011

    Posted 11 Sep 2011 Link to this post

    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
  2. Perlom
    Perlom avatar
    33 posts
    Member since:
    Aug 2011

    Posted 12 Sep 2011 Link to this post

    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}"
    




  3. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 16 Sep 2011 Link to this post

    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 >>

  4. Perlom
    Perlom avatar
    33 posts
    Member since:
    Aug 2011

    Posted 16 Sep 2011 Link to this post

    Thanks Pavel, this was helpful :)


  5. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 30 Jan 2015 in reply to Perlom Link to this post

    Is there any way to get the stackpanel to render when exporting to Excel?

    thanks.

    Eliot
  6. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 30 Jan 2015 in reply to Eliot Link to this post

    Never mind.

    I subclassed the StackPanel and overrode ToString()
  7. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 02 Feb 2015 Link to this post

    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.

     
  8. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 09 Feb 2015 in reply to Stefan Link to this post

    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




  9. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 10 Feb 2015 Link to this post

    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.

     
  10. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 11 Feb 2015 in reply to Stefan Link to this post

    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
  11. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 12 Feb 2015 Link to this post

    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.

     
  12. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 20 Feb 2015 in reply to Dimitrina Link to this post

    Hi Dimitrina,

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

    thanks for your help,

    Eliot
Back to Top