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

Sum Function Aggregate in Code Behind - Grid

2 Answers 396 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 13 May 2010, 04:51 PM

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.

2 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 19 May 2010, 09:42 AM
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.


Regards,
Veskoni
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.
0
Ratna
Top achievements
Rank 1
answered on 07 Oct 2011, 02:21 PM
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"
                                             ShowGroupFooters="True">                            
                            <telerik:RadGridView.Columns>
                                <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}" />                                     
                            </telerik:RadGridView.Columns>
                        </telerik:RadGridView>

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

            price.AggregateFunctions.Add(sumfunction);
            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();
            SetSourceField(); 
            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.)

Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Ratna
Top achievements
Rank 1
Share this question
or