Understanding Advanced Summaries in the Silverlight XamGrid

Kiril Matev / Friday, May 21, 2010

I will be writing a few blog posts which will showcase the depth of the features of the XamGrid. They will introduce you to powerful features of the XamGrid, and ultimately help you get the most of your time using our product. In this post, I’ll describe how to do advanced summaries in the XamGrid.

So what do we mean by summaries in a grid context? Summaries allow the user to get an overview of the data quickly, before decising where to focus their further research. So how do you provide this type of advanced functionality using the XamGrid? Well, implementing summaries in the XamGrid is rather straightforward - consult the Row Summaries sample under the XamGrid NetAdvantage for Silverlight 10.1 Samples.

However, if we want to go beyond the basic use cases, the XamGrid offers us a rich way of setting up summaries to suit our needs. In this blog post, we’ll examine the following:

  1. Using built-in summary calculators
  2. Implementing your own summary calculator
  3. Formatting summary values 

We'll go into how to implement each of these summaries below.

Enabling summaries in the XamGrid

In order to enable summaries in your XAML code, you need the following code segment within the XamGrid. We also set a style for any text to appear in the summary row in the code below:

            <igGrid:XamGrid.SummaryRowSettings>
                <igGrid:SummaryRowSettings AllowSummaryRow="Bottom" Style="{StaticResource SummaryRowCellsStyle}" />
            </igGrid:XamGrid.SummaryRowSettings>
Once you do this, a summary button will be shown in every column header in the XamGrid. If you’d like to disable summaries for a particular column, you can set its IsSummable property to false. You can still specify a summary for such a column, which allows you to present the user with a summary you specify rather than let them choose one on their own.

Built-in Summary Operands

Built-in summaries allow you to compute the most frequently used summaries on a column – average, count, maximum, minimum and sum. The IsApplied property is set to true to specify that the summary is calculated and displayed. The SelectionDisplayLabel property stores the text value to be displayed in the summary selector popup displayed when the user clicks the summary selector button in a column. This case is demonstrated by the Units column.

Implementing a custom summary

Oftentimes, however, the built-in summaries  do not provide the summary you need. Let’s say we want to display the mean together with the standard deviation of a numeric sample. Currently, there’s no built-in summary operand calculating these statistics, so we’ll have to write our own calculator. In order to implement that we’ve added our own SummaryOperand , as a descendant of the Infragistics.Silverlight.SummaryOperandBase class. Furthermore, we have to override the SummaryCalculator member, and return our own BasicStatisticsSummaryCalculator calculator which will perform the calculations for us. 

We’ve also added the BasicStatisticsSummaryCalculator class as a descendant of the SynchronousSummaryCalculator. We have to override the base Summarize method, and implement our own logic on calculation and result formatting:

        public override object Summarize(IQueryable data, string fieldKey)
        {
            IQueryable<SalesRecord> convertedData = (IQueryable<SalesRecord>)data;

            //get the mean
            double mean = (from s in convertedData
                             select s.PercentOfTargetDollarSales).Average();
            //get the st.dev.
            double stdev = StandardDeviation((IEnumerable<SalesRecord>)data);
            return string.Format("Mean: {0:0,0.00}\nSt.Dev: {1:0,0.00}", mean, stdev);
        }

Please note that we are in control of how the summary values appear – we can add our own text headings, and we can format the summary values.

Using our own BasicStatisticsSummaryOperand is easy – we follow the pattern of use of the built-in operands.

A reasonable question arises when using a summary operand in a template column, where you can combine in the same cell values from multiple columns: which data value does the summary operand process in its summary calculations? The data source attribute used in the summary calculations is the one that the Key property of the TemplateColumn is pointing to.

Formatting summary values

When we enabled summaries in the very first code segment in this post, we also included a reference to a style to be used in the summary area. The style is defined as follows:

        <igPrim:SummaryResultFormatStringValueConverter x:Key="SDFormatStringConverter"></igPrim:SummaryResultFormatStringValueConverter>
        <Style x:Key="SummaryRowCellsStyle" TargetType="igPrim:SummaryRowCellControl">
            <Setter Property="SummaryDisplayTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Right" FontWeight="Bold" Text="{Binding Converter={StaticResource SDFormatStringConverter}}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

The SDFormatStringConverter is an instance of the SummaryResultFormatStringValueConverter, which outputs the summary information. The template allows us to completely customize the appearance of the summary values in every summary cell.

Updating summary values in response to cell edit

Please edit any of the values in the Units column and note that the summary value is updated in response to that. Automatic updates of summary values are not built-in for performance reasons. In order to setup the XamGrid to update the summary value, handle the CellExitedEditMode event, and in the handler, invoke the InvalidateData member of the XamGrid. This would cause the XamGrid to regenerate the summary values to reflect the edit operation.

Summary

Summing it up, we looked at both built-in and custom summary operands, and using source code listings, showed how easy it is to use the powerful facilities of the XamGrid when computing and styling summaries. I invite you to download the source code, experiment with it, and adapt it to your scenarios.

If you have any questions, do not hesitate to email me at kmatev@infragistics.com