Creating Footer and Group Summaries
- 4 minutes to read
Setting footer and group summaries is a similar process. We will create a footer summary based on the SummaryFooterDemo. Let us calculate the sum of payments for the tvOrders detail View.
At design time, footer and group summaries can be specified in two ways:
by using the Component Editor to manage summary items for all columns within a (Banded) Table View;
by setting the Summary property for particular columns.
The first method involves the following steps:
Open the Component Editor and switch to the Summary panel.
Click the tvOrders label in the left panel to select this View. The label at the top of the right panel should indicate: Selected View: tvOrders.
Press the Add button on the Footer panel to add a new summary. This adds a TcxGridDBTableSummaryItem object to the FooterSummaryItems collection.
Select the newly created item and open the Object Inspector window to display its properties. Alternatively, you can click a cell, which displays the created summary item’s values.
Set the item’s properties as follows:
set the Column property to tvOrdersPaymentAmount. This specifies the column by which the summary can be calculated and where the result should be placed.
set the Kind property to skSum
set the Format property to ‘SUM = $,0.00;-$,0.00’. This property specifies the pattern for displaying the summary string. The 0.00 part will be substituted with the summary result. The two zeros following the point identify the number of digits in the summary result to the right of the decimal point. A comma stands for a thousands separator. A semicolon separates two patterns used to display positive and negative results respectively. Other characters do not have special meaning and display onscreen as is.
The FieldName property can also be used to address the column by which to evaluate a summary. If both Column and FieldName are specified, the result of the summary calculated by FieldName values will be displayed in the Column footer region.
Creating a footer summary at runtime is listed below:
with tvOrders.DataController.Summary do
begin
BeginUpdate;
try
with FooterSummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := tvOrdersPaymentAmount;
Kind := skSum;
Format := 'SUM = $,0.00;-$,0.00';
end;
finally
EndUpdate;
end;
//Update all open details of the master View (tvCars)
tvCars.DataController.ClearDetails;
end;
The second and most convenient way is to specify footer and group summaries for a particular column. This involves the following steps:
- Select the tvOrdersPaymentAmount column. You can do this using the Object TreeView or by expanding the detail View (tvOrders) or by clicking the tvOrdersPaymentAmount column in the View’s header panel.
Or open the Component Editor and click the tvOrders label in the left panel to select this View. The label at the top of the right panel should indicate: Selected View: tvOrders.and select the tvOrdersPaymentAmount column in the Columns list.
- Open the Object Inspector window to display the column’s properties. Set its properties as follows:
set the FooterKind property to skSum
set the FooterFormat property to ‘SUM = $,0.00;-$,0.00’ to specify the pattern used for displaying the summary string.
This automatically adds a TcxGridDBTableSummaryItem object to the FooterSummaryItems collection and sets its Column and Kind properties to the values which you entered/selected manually using the first method.
(The image below assumes that there are no existing footer summaries within this View.)
The summary item’s Position property is set to the spFooter in the same way as the FooterKind and FooterFormat properties were set.
You might need to set the item’s FieldName property in the Component Editor to address the column which a summary is evaluated for, since the second method assumes that the summary is calculated for the column you selected and thus leaves the FieldName property unchanged.
Creating a footer summary by this method at runtime is listed below:
with tvOrdersPaymentAmount.Summary do
begin
FooterKind := skSum;
FooterFormat := 'SUM = $,0.00;-$,0.00';
end;
This code is sufficient to display the summary by the tvOrdersPaymentAmount values under this column in the footer panel of the tvOrders View.
The following image shows the result of the footer summary calculation in a detail View:
As for the group summary, you can set the position where a summary will be displayed by using the Position property or specify the corresponding column’s GroupKind or GroupFooterKind properties. A group summary can be displayed either in a group row or in a group footer panel.