I have three columns: A, B, and C. Each column has a summary row with a "Sum = ". I would like to include a summary for B and C that is a percent of A. In other words I'd like a summary in column B to include sum(B)/sum(A) and in column C to include sum(C)/sum(A). Is this possible?
Hello,
After creating a class that inherits and implements SummaryCalculator, all you need to do is in the method Aggregate make an instance of the grid and retrieve the summary results of the other calculators. Here is some sample source code for this:
public class CustomCalc : SummaryCalculator { double result; decimal tempA; decimal tempB; decimal tempC; public override void Aggregate(object dataValue, SummaryResult summaryResult, Record record) { XamDataGrid grid = record.ParentCollection.DataPresenter as XamDataGrid;
tempA = decimal.Parse(grid.Records.SummaryResults[0].Value.ToString());
tempB = decimal.Parse(grid.Records.SummaryResults[1].Value.ToString());
tempC = (decimal)tempC + Convert.ToDecimal(dataValue); //calculating the C column calculator
} public override void BeginCalculation(SummaryResult summaryResult) { result = 0; } public override bool CanProcessDataType(Type dataType) { return Infragistics.Windows.Utilities.IsNumericType(dataType); } public override string Description { get { return "A Column Sum divided by C Column Sum"; } } public override object EndCalculation(SummaryResult summaryResult) { tempB = tempB; return (double)tempA/(double)tempC; // result! } public override string Name { get { return "A div C"; } } public override bool IsCalculationAffectedBySort { get { return false; //Returning MyBase.IsCalculationAffectedBySort will have the same result as returning False. } } } private void xamDataGrid1_SummaryResultChanged(object sender, Infragistics.Windows.DataPresenter.Events.SummaryResultChangedEventArgs e) { string field = null; field = e.SummaryResult.SourceField.Label.ToString().ToLower(); switch (field) { case "a": { columnA = int.Parse(e.SummaryResult.Value.ToString()); return; } case "b": { columnB = int.Parse(e.SummaryResult.Value.ToString()); return; } case "c": { //columnC = int.Parse(e.SummaryResult.Value.ToString()); return; } default: return; } } private void Window_Loaded_1(object sender, RoutedEventArgs e) { SummaryCalculator.Register(new CustomCalc()); //registering the calculator }
And here is the xaml code a sceen shot of the sample :
<Window.Resources> <XmlDataProvider x:Key="NumberData" XPath="/Numbers"> <x:XData> <Numbers xmlns=""> <Number A="10" B="20" C="15"/> <Number A="20" B="30" C="25"/> <Number A="30" B="40" C="35"/> <Number A="49" B="50" C="45"/> </Numbers> </x:XData> </XmlDataProvider> </Window.Resources> <Grid> <!--DataSource="{Binding Source={StaticResource NumberData},XPath=Number}"--> <igDP:XamDataGrid Margin="0,12,12,30" Name="xamDataGrid1" DataSource="{Binding Source={StaticResource NumberData},XPath=Number}" SummaryResultChanged="xamDataGrid1_SummaryResultChanged"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings AllowSummaries="true" SummaryUIType="MultiSelect"/> </igDP:XamDataGrid.FieldSettings><igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.SummaryDefinitions> <igDP:SummaryDefinition Key="idp_sumA" SourceFieldName="A" Calculator="Sum"/> <igDP:SummaryDefinition Key="idp_sumB" SourceFieldName="B" Calculator="Sum"/> <igDP:SummaryDefinition Key="idp_sumC" SourceFieldName="C" Calculator="Sum"/> </igDP:FieldLayout.SummaryDefinitions> <igDP:FieldLayout.Fields> <igDP:Field Name="A" Label="A"> <igDP:Field.Settings> <igDP:FieldSettings EditAsType="{x:Type sys:Int32}" /> </igDP:Field.Settings> </igDP:Field> <igDP:Field Name="B" Label="B"> <igDP:Field.Settings> <igDP:FieldSettings EditAsType="{x:Type sys:Int32}" /> </igDP:Field.Settings> </igDP:Field> <igDP:Field Name="C" Label="C"> <igDP:Field.Settings> <igDP:FieldSettings EditAsType="{x:Type sys:Int32}" /> </igDP:Field.Settings> </igDP:Field> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid> </Grid>
I had already reviewed the custom calculator help and found it helpful. But I didn't see anything that would allow me to include more than one column's sum in the calculation. I need the results to display the sum of column B divided by the sum of column A. So if the custom sum is running on column B how would I get column A's sum? Then, to make it more difficult, how would I pass column A to the calculator so that it knew column A was the additional column I was interested in?
Hello Ian,
Row Summaries indeed have five built-in functions - Sum, Avg, Min, Max, Count. You can however extend the Row Summaries model with your custom row summary - there is a great online help article located here that demonstrates this with code details:
http://help.infragistics.com/Help/NetAdvantage/WPF/2008.2/CLR3.X/html/xamDataGrid_Creating_a_Custom_Summary_Calculator.html
Hope this helps.