The XamDataGrid Row Summary functionality allows for a lot of flexibility. It enables developers to easily expose basic summary information to their consumers with a simple property setting as demonstrated here. In some situations, a developer may want to either have more control over a summary result or create their own business specific formula.
This article will look at information stored in a DataGrid and given a particular sales person calculate who sold the most for a given year and then integrate that summary into the xamDataGrid.
To begin, create a custom class titled TopSalesPerson and add to that class two imports/using statements and inherit from SummaryCalculator:
using Infragistics.Windows.DataPresenter; using Infragistics.Windows; … namespace XamCommunitySample { class TopSalesPerson : SummaryCalculator { private string _currentTopSalesPerson; private double _currentTopSalesAmount; } }
Next implement the BeginCalculation Method to initialize your variables:
public override void BeginCalculation(SummaryResult summaryResult) { _currentTopSalesAmount = 0; _currentTopSalesPerson = string.Empty; }
Next, implement the CanProcessDataType to tell the calculator to only calculate for numeric types:
public override bool CanProcessDataType(Type dataType) { return Utilities.IsNumericType(dataType); }
The next part deals with the core part of our custom business logic in the aggregate method. The aggregate method essentially loops over every record in the xamDataGrid and gives you access to that Record as well as to the value in the current calculating column for that particular record. For our sample, we’re going to keep tabs on the current sales leader in a given category and as we find a leader update our string with that person’s name.
public override void Aggregate(object dataValue, SummaryResult summaryResult, Record record) { double myCellValue = double.Parse(dataValue.ToString()); DataRecord myCurrentRecord = record as DataRecord; if (myCellValue > _currentTopSalesAmount) { _currentTopSalesAmount = myCellValue; _currentTopSalesPerson = myCurrentRecord.Cells["FirstName"].Value.ToString() + " " + myCurrentRecord.Cells["LastName"].Value.ToString(); } }
After we’ve implemented this method, we only have a couple of steps in our calculation class to go. The first one, is to return the Winning Sales Persons name. The second is to name our custom calculation function and give it a description. The name is what will display in the dropdown box at run-time in the datagrid.
public override object EndCalculation(SummaryResult summaryResult) { return _currentTopSalesPerson; } public override string Name { get { return "Sales Leader"; } } public override string Description { get { return "Top Sales Person"; } }
Almost finished, now we can assign our new custom summary to our application by inserting it into our window_loaded method:
private void Window_Loaded(object sender, RoutedEventArgs e) { TopSalesPerson Summary = new TopSalesPerson(); SummaryCalculator.Register(Summary); }
Lastly, the developer may want to show their custom summary on a specific field without placing it on every column. For instance, we may have a Quota column and a Sales column; the end-user would expect to see who the top sales person was, but maybe not who had the highest quota. This can be achieved by not registering our summary like demonstrated above, but by associating a specific summary definition with a given column:
TopSalesPerson Summary = new TopSalesPerson(); SummaryDefinition myTopSalesPersonFormula = new SummaryDefinition(); myTopSalesPersonFormula.SourceFieldName = "SalesYTD"; myTopSalesPersonFormula.Calculator = Summary; this.xamDataGrid1.FieldLayouts[0].SummaryDefinitions.Add(myTopSalesPersonFormula);
In closing, the summary capabilities of the DataPresenter off ease of use in the pre-built commonly used formulas and flexibility in the ability to quickly add custom formulas to the DataGrid.