Custom Summary
- 3 minutes to read
Total summaries and group summaries include five predefined aggregate functions that allow you to calculate:
- The number of data rows
- The maximum and minimum values
- The sum and the average value
The ASPxGridBase.CustomSummaryCalculate event allows you to implement a custom summary. For example, custom summaries can calculate summaries against records that meet specific criteria and use multiple data fields in calculations.
Implement Custom Summary
Follow the steps below to implement a custom summary:
- Create a summary item and set its ASPxSummaryItemBase.SummaryType property to SummaryItemType.Custom.
- Handle the ASPxGridBase.CustomSummaryCalculate event to calculate the summary.
The ASPxGridBase.CustomSummaryCalculate event fires for grid data rows in the following cases:
When you calculate a total summary value, the grid raises this event for each data row.
When you calculate a group summary value, the grid raises this event for each data row in a group.
Note
Note that the grid raises the ASPxGridBase.CustomSummaryCalculate event before and after a row is processed.
The summary calculation consists of three stages:
Initialization
The grid raises the ASPxGridBase.CustomSummaryCalculate event once at this stage. The SummaryProcess property value is Start. At this stage, the control initializes the summary value (for example, resets internal counters).
Calculation
The grid raises the ASPxGridBase.CustomSummaryCalculate event multiple times at this stage - for each data row in a grid or group. The SummaryProcess property value is Calculate. At this stage, the control calculates the summary value.
You can set the TotalValueReady property to
true
at the Initialization stage to skip the Calculation stage and calculate a custom summary at the Initialization or Finalization stage.Finalization
The grid raises the ASPxGridBase.CustomSummaryCalculate event once at this stage. The SummaryProcess property value is Finalize. At this stage, the control assigns the calculated summary value to the TotalValue property.
Example
<dx:ASPxGridView ID="ASPxGridView1" runat="server" OnCustomCallback="ASPxGridView1_CustomCallback" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" ClientInstanceName="grid" Theme="Office365" KeyFieldName="ProductID" OnCustomSummaryCalculate="ASPxGridView1_CustomSummaryCalculate">
<Columns>
<dx:GridViewCommandColumn SelectAllCheckboxMode="Page" ShowSelectCheckbox="True" VisibleIndex="0">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="ProductID" VisibleIndex="1" ReadOnly="True">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="3">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowFooter="true" />
<TotalSummary>
<dx:ASPxSummaryItem SummaryType="Custom" FieldName="UnitPrice" ShowInColumn="UnitPrice" DisplayFormat="Total price for selected products: {0:c}" ValueDisplayFormat="{0}:c" />
</TotalSummary>
</dx:ASPxGridView>
<br /> <br />
<dx:ASPxButton ID="ASPxButton4" runat="server" Text="Refresh" AutoPostBack="false" Theme="Office365">
<ClientSideEvents Click="OnClick" />
</dx:ASPxButton>
Result: