WPF DataGrid footer and aggregates

Our WPF DataGrid control has just received two great new features based on customer suggestions: a customizable footer, and built in aggregate calculations. The footer provides a way to display cells of content along the bottom of the DataGrid that line up with particular columns. The built in aggregates calculate useful values such as sum, count, average and more which can then be displayed for each column either in the footer or in group headers.

Footer

Footer aggregates

To use the new DataGrid footer feature, the first thing to do is display it by setting the DataGrid.IsFooterVisible property to true. To make use of the footer, you’ll then want to display something for at least one of the columns. This can be done by setting the FooterTemplate on any of the columns you need. The data context of the template is a DataGridFooterCell which has Column and Aggregate properties that you can bind to. Using the flexibility of WPF data templates, you can display all sorts of different things:

  • Static content such as images and labels
  • Buttons or other simple controls to trigger commands for the column
  • Data bind to display dynamic information for the column
  • Template the aggregate results in a different way to the default template

Handy tip: The DataGridColumn class has a Tag property which you can use to pass values to the footer template. This can be done to display dynamically changing values or command parameters etc.

Aggregates

Group aggregates

Aggregates calculate useful values for either groups or the entire items source. WPF Elements includes the following aggregates: count, sum, minimum, maximum, average and mode (most common value). You can also extend the AggregateBase class to create your own custom aggregate calculations. DataGridColumn provides two self-explanatory properties for setting aggregates: FooterAggregate and GroupAggregate. You can set one or both of these properties, and they don’t need to be the same type of aggregate – you can do whatever is required for your application. Below is an example of setting up aggregates on a DataGridColumn. When you are setting an aggregate, Visual Studio intellisense will show you all the available aggregates – so they are easy to find.

<ms:DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="True" IsFooterVisible="True">
  <ms:DataGridColumn PropertyName="Age">
    <ms:DataGridColumn.FooterAggregate>
      <ms:MaximumAggregate />
    </ms:DataGridColumn.FooterAggregate>
    <ms:DataGridColumn.GroupAggregate>
      <ms:AverageAggregate />
    </ms:DataGridColumn.GroupAggregate>
  </ms:DataGridColumn>
</DataGrid>

If you do not set a FooterTemplate as described in the previous section, then a template for displaying the aggregate will be conveniently selected for you. If you do set a FooterTemplate, you can either ignore displaying the aggregate, or you could change the way the aggregate is displayed. For example, you could change the font color based on if an AverageAggregate result is too high, too low or within a safe threshold. Similarly, you can set the GroupHeaderCellTemplate of a column to customize the look of cells in a group header. Again this could be used to display static values, controls, dynamic information or your own custom view of the aggregate result. The data context of this template is a GroupHeaderCell which has properties to bind to the DataGridColumn, the DataGridGroup and the IAggregate. GroupHeaderCell is very similar to FooterCell making it very easy to create a single template that works the same in both the group headers and the footer.

Dynamic updates

The aggregate feature goes very well with the filtering feature that was released a little while ago. When a filter is applied, all the footer and group aggregate results will dynamically update to only include the filtered results. For example, a CountAggregate will now display the number of items that returned from the filtering expression. A ModeAggregate could show the percentage of males or females within a filtered age range – and all sorts of other useful scenarios. Another thing to point out is that the aggregate results will dynamically update while the user edits values in the data grid. If grouping is enabled, only the groups visible on the screen will have their aggregates updated, rather than all groups in the DataGrid. This means the aggregate results update seamlessly as the user edits values without feeling any impact on performance.

Get these features now!

The DataGrid footer and built in aggregates are available right now in the current nightly build. If you have a trial version, then you can get the latest nightly from here. Or if you are already a customer, grab a copy from your account page. If you have any questions or suggestions about these new feature, we’ll be over in the forum to help you out.

Tagged as WPF Elements

2 Responses to “WPF DataGrid footer and aggregates”

Archives

Join our mailer

You should join our newsletter! Sent monthly:

Back to Top