# Custom Summaries | WinForms Controls | DevExpress Documentation

This topic describes how to create custom summaries.

## Custom Summaries Overview

Pivot Grid allows you to calculate [common summary functions](/WindowsForms/9390/controls-and-libraries/pivot-grid/data-shaping/summarization/summaries/automatic-summaries) for data fields. You can create custom summaries to execute complex calculations. 

Custom summaries can do the following:

- Use predefined and [custom aggregate](/CoreLibraries/403494/devexpress-pivot-grid-core-library/advanced-analytics/custom-aggregate-functions) functions to calculate summary values.
- Involve multiple fields in a summary calculation.
- Calculate a summary for records that match certain criteria.

## Create a Custom Summary in Optimized and Server Modes

Do the following to create a custom summary for a data field: 

1. Create a Pivot Grid field.
2. Create an [ExpressionDataBinding](/WindowsForms/DevExpress.XtraPivotGrid.ExpressionDataBinding) object, and specify a calculated summary expression in the object’s constructor.
3. Assign the created `ExpressionDataBinding` object to the field’s [DataBinding](/CoreLibraries/DevExpress.XtraPivotGrid.PivotGridFieldBase.DataBinding) property.
4. Add the field to the [Data Area](/WindowsForms/1693/controls-and-libraries/pivot-grid/ui-elements/data-area).

Refer to the following article for information on how to create calculated expressions: [Calculated Fields](/WindowsForms/1799/controls-and-libraries/pivot-grid/binding-to-data/in-memory-mode/Optimized-Mode/bind-pivot-grid-fields-to-calculated-expressions).

Tip

If a custom summary calculation requires data from multiple data sources, bind the Pivot Grid to a [FederationDataSource](/CoreLibraries/DevExpress.DataAccess.DataFederation.FederationDataSource) that combines data from two or more tables into one data set. The custom summary expression can then use fields defined in the `FederationDataSource`.

### Example

The following example specifies a custom summary for the *First Product Sold* field. The custom summary’s expression (`FirstValue([ProductName])`) uses a custom aggregate function (`FirstValue`) to return the first product sold by a sales person in each product category.

![First Value Custom Function Example](/WindowsForms/images/win-pivot-first-value-function.png)

[View Example](https://github.com/DevExpress-Examples/winforms-pivot-grid-custom-aggregates)

- Form1.cs
- Form1.vb

<section id="tabpanel_QENkOi3DoW_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code data-code-links="{&quot;/ (DevExpress.XtraPivotGrid)(?:;|$)/&quot;:&quot;/WindowsForms/DevExpress.XtraPivotGrid&quot;,&quot;/ (System.Windows.Forms)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows.forms&quot;}" class="lang-cs">using DevExpress.XtraPivotGrid;
using System.Windows.Forms;

namespace WinPivot_CustomFunctions {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();     
            //  ...
            pivotGridControl1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized;
            PivotGridField pivotGridField1 = new PivotGridField() {
                Area = PivotArea.DataArea,
                AreaIndex = 0,
                Caption = &quot;First Product Sold&quot;,
                FieldName = &quot;FirstProductSold&quot;
            };
            pivotGridControl1.Fields.Add(pivotGridField1);
            pivotGridField1.DataBinding = new ExpressionDataBinding() { 
                Expression = &quot;FirstValue([ProductName])&quot; };
            pivotGridField1.Options.ShowExpressionEditorMenu = true;
            pivotGridField1.Options.ShowGrandTotal = false; 
        }      
    }
}
</code></pre></section>
<section id="tabpanel_QENkOi3DoW_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;/ (System.Windows.Forms)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows.forms&quot;,&quot;/ (DevExpress.XtraPivotGrid)(?:;|$)/&quot;:&quot;/WindowsForms/DevExpress.XtraPivotGrid&quot;}" class="lang-vb">Imports System.Windows.Forms
Imports DevExpress.XtraPivotGrid

Namespace WinPivot_CustomFunctions
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
            &#39;  ...
            pivotGridControl1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized
            Dim pivotGridField1 As New PivotGridField() With {
                .Area = PivotArea.DataArea,
                .AreaIndex = 0,
                .Caption = &quot;First Product Sold&quot;,
                .FieldName = &quot;FirstProductSold&quot;
            }
            pivotGridControl1.Fields.Add(pivotGridField1)
            pivotGridField1.DataBinding = New ExpressionDataBinding() With {.Expression = &quot;FirstValue([ProductName])&quot;}
            pivotGridField1.Options.ShowExpressionEditorMenu = True
            pivotGridField1.Options.ShowGrandTotal = False
        End Sub
    End Class
End Namespace
</code></pre></section>

## Create a Custom Summary in Legacy and LegacyOptimized Modes

Use the [PivotGridControl.CustomSummary](/WindowsForms/DevExpress.XtraPivotGrid.PivotGridControl.CustomSummary) event to create custom summaries in **Legacy** or **LegacyOptimized** mode.

## More Documentation

Refer to the following articles for more information about custom functions:

- [Custom Functions](/WindowsForms/9947/common-features/expressions/implementing-custom-functions)
- [Custom Aggregate Functions](/CoreLibraries/403494/devexpress-pivot-grid-core-library/advanced-analytics/custom-aggregate-functions)

See Also

[Automatic Summaries](/WindowsForms/9390/controls-and-libraries/pivot-grid/data-shaping/summarization/summaries/automatic-summaries)

[Summaries Overview](/WindowsForms/1812/controls-and-libraries/pivot-grid/data-shaping/summarization/summaries/summaries-overview)

[Calculate and Display Custom Summaries When Pivot Grid Operates in Different Data Processing Modes](https://github.com/DevExpress-Examples/winforms-pivotgrid--display-the-difference-of-income-and-outlay-in-totals)