Using the C1FlexGrid Control > Outlining and Summarizing Data > Using the Subtotal Method |
We mentioned earlier that you could also create trees using the C1FlexGrid's Subtotal method. TheSubtotal method performs the same tasks as the GroupBy and AddSubtotals methods described above, except it does both things in a single step and is therefore a little more efficient.
The code below shows how you can use the Subtotal method to accomplish the same thing we did before, only a little faster and without using any helper methods:
To write code in C#
C# |
Copy Code
|
---|---|
void _btnTreeCountryCity_Click(object sender, EventArgs e) { using (new DeferRefresh(_flex)) { // restore original sort (by Country, City, SalesPerson) ResetBinding(); // group and total by country and city _flex.Subtotal(AggregateEnum.Sum, 0, "Country", "ExtendedPrice"); _flex.Subtotal(AggregateEnum.Sum, 0, "Country", "Quantity"); _flex.Subtotal(AggregateEnum.Sum, 1, "City", "ExtendedPrice"); _flex.Subtotal(AggregateEnum.Sum, 1, "City", "Quantity"); // hide columns that we grouped on // (they only have duplicate values which already appear on the tree nodes) // (but don't make them invisible, that would also hide the node text) _flex.Cols["Country"].Width = 0; _flex.Cols["City"].Width = 0; _flex.AllowMerging = AllowMergingEnum.Nodes; // show outline tree _flex.Tree.Column = 0; _flex.AutoSizeCol(_flex.Tree.Column); _flex.Tree.Show(1); } } |
The Subtotal method is very convenient and flexible. It has a number of overloads that allow you to specify which columns should be grouped on and totaled on by index or by name, whether to include a caption in the node rows that it inserts, how to perform the grouping, and so on. The summary below describes the overloads available:
This version of the method takes only one aggregate type as a parameter. It is useful only for removing existing subtotals before inserting new ones. In this case, the aggType parameter is set to AggregateEnum.Clear.
Subtotal(AggregateEnum aggType, string groupBy, string totalOn)
These are the most commonly used overloads. The parameters are the type of aggregate to insert and the columns to group by and total on. The columns may be referenced by index or by name. The latter is the one we used in the example above.
Subtotal(AggregateEnum aggType, string groupBy, string totalOn, string caption)
These overloads add an extra caption parameter. The caption parameter determines the text that is added to the new node rows to identify the value being grouped on. By default, the value being grouped on is shown, so if you are grouping by country, the node rows would show "Argentina", "Brazil", and so on. If you set the caption parameter to a string such as "Country: {0}", then the node rows would show "Country: Argentina" instead.
Subtotal(AggregateEnum aggType, string groupFrom, string groupTo, string totalOn, string caption)
These overloads separate the groupBy parameter into two: groupFrom and groupTo. By default, the Subtotal method inserts a node row whenever the value of the groupBy or any previous column changes.
For example, if you a row has the same value in the "City" column as the previous row, but a different value in the "Country" column, then the Subtotal method assumes the rows should be in different groups and inserts a new node row even though the value of the groupBy column is the same. These aggregates let you override that behavior and specify the range of columns that should be considered when identifying a group.