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
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}"
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 >>
thanks.
Eliot
I subclassed the StackPanel and overrode ToString()
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.
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
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.
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
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.
I ended up saving it in CSV format.
This preserved the line breaks.
thanks for your help,
Eliot