Sum Function Aggregate in Code Behind - Grid

3 posts, 0 answers
  1. Michael
    Michael avatar
    5 posts
    Member since:
    Aug 2008

    Posted 13 May 2010 Link to this post

    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" > 
            <TextBlock Text="Prv Yr Neg Amt" Style="{StaticResource HeaderCellTextBlockStyle}">  
                      <ToolTip Content="Prv Yr Neg Amt"></ToolTip> 
            <Data:SumFunction Caption="Sum (Prev Yr Neg Amt):" ResultFormatString="{}{0:c0}" /> 

    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.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.
  2. Veselin Vasilev
    Veselin Vasilev avatar
    2992 posts

    Posted 19 May 2010 Link to this post

    Hi Michael,

    I tried to reproduce the problem in a sample project but was not able to.

    Please find it attached and let me know what changes should I make in order to observe the unwanted behavior.

    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Ratna
    Ratna avatar
    2 posts
    Member since:
    Oct 2011

    Posted 07 Oct 2011 Link to this post

    I  have a radcolumn in the xaml file and i m trying to add an aggregate function during runtime.

    my xaml code is:
    <telerik:RadGridView x:Name="PunchOut" x:Uid="dgPunchOut" AutoGenerateColumns="False" Margin="5" Height="440" CanUserFreezeColumns="False" IsSynchronizedWithCurrentItem="True"
                                    <telerik:GridViewDataColumn UniqueName="PartNumber" Header="Part Number" Width="145"  DataMemberBinding="{Binding PartNumber}" IsReadOnly="True" />
                                    <telerik:GridViewDataColumn UniqueName="Price" Header="Price" DataMemberBinding="{Binding Price}" Width="100" IsReadOnly="True" />
                                    <telerik:GridViewDataColumn UniqueName="SelectedQuantity" Header="SelectedQuantity" Width="45"  IsReadOnly="False" DataMemberBinding="{Binding SelectedQuantity}" />                                     

    Here, for price column, im adding aggregate function in code:
     public void SetSourceField()
                SumFunction sumfunction = new SumFunction();
                GridViewColumn price = new GridViewColumn();
                price = PunchOut.Columns["Price"];

                sumfunction.SourceField = "Price";
                sumfunction.ResultFormatString = "{}Total:{0:c}";

                this.PunchOut.Rebind(); // i dont know if this is necessary

    i bind it during a call to the function:
      private void GetItemsCompleted(object sender, PunchOutServiceNamespace.GetItemsCompletedEventArgs e)
                PunchOut.ItemsSource = e.Result;// this e.result comes from wcf service so i dont have access to any datasource other than through e.result();
                PunchOut.Rebind(); // punch out is the name of my gridview

    this aggregate function does not show up.

    (i cannot have the binding property set in the xaml file).
    i have to write the aggregate function only in the code behind and not in the xaml file.

    please let me know what im missing. (something like a refresh or additional insert etc.)

Back to Top