Worksheet.Subtotal(CellRange, Int32, List<Int32>, Int32, String) Method
Creates subtotals for the specified range of cells.
A CellRange object that is the range of cells for which the subtotals should be created.
An integer that specifies the zero-based index of a column containing related data for grouping.
A list of column indexes in a worksheet that defines columns for which the subtotals should be calculated.
An integer that specifies the code of the function to be used in calculating subtotals.
A string that defines the text to be displayed in the summary rows.
Before subtotaling your data, make sure that the range you wish to subtotal has columns headings and the column defined by the groupByColumn parameter contains repetitive values. Each time a value in this column changes, a new subtotal row is inserted, so that it is better to sort your data to ensure that the same values in the groupByColumn column will be in one group.
The table below provides a full list of functions that can be used in calculating subtotals.
(includes hidden values)
(ignores hidden values)
Before subtotaling, make sure that the range you wish to subtotal has column headings in the first row and the data in this range is sorted by a column at each change in which a subtotal row will be inserted.
To insert subtotals, do the following.
- Specify the CellRange object, which contains data you wish to subtotal
- Create a list of column indexes that define columns for which the subtotals should be calculated. In this example, the SUBTOTAL function will be calculated only for column "D" (with Column.Index equal to 3).
Call the Worksheet.Subtotal method and pass the following parameters: the specified data range to be subtotaled, the index of the column by which you wish to group your data, the specified list of columns to which the subtotals should be added, the code of the function to be used in calculating subtotals and the text to be displayed in the summary rows.
In this example, a subtotal row will be inserted each time a value in column "B" (with Column.Index equal to 1) changes. The SUM function with code 9 is used to calculate subtotals.
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-spreadsheetcontrol-api-part-2-e4832
CellRange dataRange = worksheet["B3:E23"]; // Specify that subtotals should be calculated for the column "D". List<int> subtotalColumnsList = new List<int>(); subtotalColumnsList.Add(3); // Insert subtotals by each change in the column "B" and calculate the SUM fuction for the related rows in the column "D". worksheet.Subtotal(dataRange, 1, subtotalColumnsList, 9, "Total");