Creating Summary Groups
- 4 minutes to read
The SummaryGroupDemo demonstrates summary group calculation. This demo displays Orders table data from the CarsDB database. When grouping data by the CustomerID and ProductID columns, different summaries are calculated.
Let us examine the steps for creating two summary groups. The first summary group will calculate Sum and Count summaries using the PaymentAmount column when data is grouped by CustomerID. The second group also specifies two summaries to calculate, but this time for when grouping is by the ProductID column. In this instance, Sum and Minimum summaries are evaluated by the OrdersQuantity and PurchaseDate columns, respectively.
Open the Component Editor and switch to the Groups panel for the tvOrders View. (The images below assume that there were no existing summary groups within this View.)
Click the Add button to create a new group. A summary group represents an object of the TcxDataSummaryGroup class associating a collection of proposed grouping columns with summaries to calculate (a collection of TcxDataSummaryItem objects).
When a summary group is created, the Unlinked Columns panel is populated with the columns of the current View.
- Select the CustomerID column from the Unlinked Columns panel and click the ‘>‘ button to move it to the Linked Columns group.
- Create a summary in the Items panel by clicking the corresponding Add button.
- Select it via the mouse and open the Object Inspector window to display this item’s properties. Set its properties as follows:
- To create the second summary, click the Add button on the Items panel once more and adjust its properties as displayed in the following image:
The first summary group is completed. The second summary group can be created in the same manner. But instead of CustomerID, the ProductID column should be moved to the Linked Columns panel and the two summary items should reference the OrdersQuantity and PurchaseDate columns.
The following code shows how you can create these summaries programmatically:
with tvOrders.DataController.Summary do
begin
BeginUpdate;
try
SummaryGroups.Clear;
//The first summary group
with SummaryGroups.Add do
begin
//Add proposed grouping column(s)
TcxGridTableSummaryGroupItemLink(Links.Add).Column := tvOrdersCustomerID;
//Add summary items
with SummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := tvOrdersPaymentAmount;
Kind := skSum;
Format := 'Amount Paid: $,0';
end;
with SummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := tvOrdersPaymentAmount;
Kind := skCount;
Format := 'Records: 0';
end;
end;
//The second summary group
with SummaryGroups.Add do
begin
//Add proposed grouping column(s)
TcxGridTableSummaryGroupItemLink(Links.Add).Column := tvOrdersProductID;
//Add summary items
with SummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := tvOrdersQuantity;
Kind := skSum;
Position := spFooter;
Format := 'TOTAL = 0';
end;
with SummaryItems.Add as TcxGridDBTableSummaryItem do
begin
Column := tvOrdersPurchaseDate;
Kind := skMin;
Position := spFooter;
end;
end;
finally
EndUpdate;
end;
end;
The following image shows a grid control with data grouped by the CustomerID and ProductID columns. For the first group level, the two summaries specified by the summary group linked to the CustomerID column are calculated. In the second group level, summaries are specified by the summary group linked to the ProductID column: