FlexGrid for WinForms
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#

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:

  1. Subtotal(AggregateEnum aggType)

    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.

  2. Subtotal(AggregateEnum aggType, int groupBy, int totalOn)

    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.

  3. Subtotal(AggregateEnum aggType, int groupBy, int totalOn, string caption)

    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.

  4. Subtotal(AggregateEnum aggType, int groupFrom, int groupTo, int totalOn, string caption)

    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.

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Product Support Forum |  Documentation Feedback