I am trying to define a sum aggregate in code behind for a Grid column in code behind. These fields are pulled in dynamically. For a column that is set in the XAML, I have set it like so:
<telerikGridView:GridViewDataColumn UniqueName="PreviouslyNegotiatedAmount" DataMemberBinding="{Binding PreviouslyNegotiatedAmount}" HeaderCellStyle="{StaticResource HeaderCellStyle}" DataFormatString="{}{0:c0}" IsReadOnly="True" MinWidth="60" Width="80" > |
<telerikGridView:GridViewDataColumn.Header> |
<TextBlock Text="Prv Yr Neg Amt" Style="{StaticResource HeaderCellTextBlockStyle}"> |
<ToolTipService.ToolTip> |
<ToolTip Content="Prv Yr Neg Amt"></ToolTip> |
</ToolTipService.ToolTip> |
</TextBlock> |
</telerikGridView:GridViewDataColumn.Header> |
<telerikGridView:GridViewDataColumn.AggregateFunctions> |
<Data:SumFunction Caption="Sum (Prev Yr Neg Amt):" ResultFormatString="{}{0:c0}" /> |
</telerikGridView:GridViewDataColumn.AggregateFunctions> |
</telerikGridView:GridViewDataColumn> |
When I perform a grouping it sums the data in the groups and converts it to currency correctly.
In code behind, I have the following. MatricDataColumn inherits from GridViewComboBoxColumn that simply adds a string MatricName and overrides the CreateCellElement method:
using (RadGridViewProposal.DeferRefresh()) |
{ |
var matrics = (from t in CurrentTemplate.TemplateMetrics |
orderby t.OrderOfMetric |
select t).ToList(); |
foreach (var m in matrics) |
{ |
var textBlockHeader = new TextBlock {Text = m.MatricName}; |
textBlockHeader.SetValue(ToolTipService.ToolTipProperty, m.Description); |
textBlockHeader.Style = Resources["HeaderCellTextBlockStyle"] as Style; |
var col = new MatricDataColumn |
{ |
MatricName = m.MatricName, |
DataMemberBinding = new Binding(m.MatricName + "_Value"), |
DataFormatString = m.FormatString, |
DataType = Type.GetType(m.DataType), |
UniqueName = "Matrix_" + m.MatricName, |
//AggregateFunctions = new Telerik.Windows.Data.AggregateFunctionCollection{ new Telerik.Windows.Data.AggregateFunction{ Caption= " } |
Header = textBlockHeader, |
IsFilterable = true, |
IsReadOnly = true, |
IsSortable = true, |
HeaderCellStyle = Resources["HeaderCellStyle"] as Style, |
Width = 50, |
MinWidth = 30, |
TextAlignment = TextAlignment.Center |
}; |
if (col.UniqueName == "Matrix_Red Flags") |
col.AggregateFunctions.Add(new Telerik.Windows.Data.CountFunction |
{Caption = "Count (Red Flags):"}); |
else if (col.UniqueName == "Matrix_ABC Spend") |
col.AggregateFunctions.Add(new Telerik.Windows.Data.SumFunction |
{ |
Caption = "Sum (ABC Spend):", |
SourceField = "ABC Spend_Value", |
ResultFormatString = "{}{0:c0}" |
}); |
if (m.OrderOfMetric == -1) |
{ |
RadGridViewProposal.Columns.Insert(RadGridViewProposal.FrozenColumnCount, col); |
RadGridViewProposal.FrozenColumnCount++; |
} |
else |
RadGridViewProposal.Columns.Insert(RadGridViewProposal.Columns.Count - 4, col); |
} |
} |
The CountFunction works as expected.
When grouping, the caption of the SumFunction shows up, but the value is blank. It does not error out, but I'm not sure if I have the correct SourceField specified. In the XAML I did not have to specify a SourceField. I tried this approach, but same result. Also, I believe the SourceField is correct because if I try to change it to Total Spend_Value.Text, it gives the error: there is no property Text for data type of decimal, so the Grid knows what that field is.
I've attached a screenshot of the grouping results. The amount for Prev yr Neg Amt was censored.
Any suggestions are greatly appreciated.
Please let me know should you need anything else.