wpf devexpress custom statistics

Total statistics and group statistics contain predefined total functions. These functions allow you to calculate as follows:

Number of data columns (Count)

Maximum and minimum values ​​(Max and Min)

Total and Average (Sum and Average)

Handle the GridControl.CustomSummary event or use the GridControl.CustomSummaryCommand property to apply custom rules to calculate statistics. Custom statistics allow the following operations:

Compute statistics for records and special types encountered

Calling multiple data fields in calculations

Implement complex statistical functions (for popular deviations from the standard and so on)

If the GridControl.View property is set to TreeListView, use the TreeListView.CustomSummary event or the TreeListView.CustomSummaryCommand property

General information

Calculate statistics manually:

1. Create statistical content and set the SummaryItemBase.SummaryType property to SummaryItemType.Custom

2. Create a command to use a custom algorithm to calculate the value

3. Bind the command to the GridControl.CustomSummaryCommand property

GridControl is calculated as follows:

initialization

This GridControl executes the CustomSummary command and sets the SummaryArgs.SummaryProcess property to Start. At this stage you can initialize statistical values ​​(e.g. reset internal counters).

calculate

GridControl executes the CustomSummary command multiple times, in the view and grouping, for each data column. The SummaryArgs.SummaryProcess property sets the calculation. At this stage, statistics can be calculated.

Finish

GridControl executes the CustomSummary command to set the SummaryArgs.SummaryProcess property to end. At this stage, you can assign the calculated statistics in the SummaryArgs.TotalValue property.

To ignore the Calculation phase and calculate a custom statistic during the initialization and final phases, set the SummaryArgs.TotalValueReady property to true during the initialization phase. Ignore the calculation phase and the start and end phases.

Calculate custom statistics

The following code example calculates the total number of empty cells in a specific row:

<dxg:GridControl ItemsSource="{Binding Items}"
                 CustomSummaryCommand="{Binding CustomSummaryCommand}">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Text" GroupIndex="0" />
        <dxg:GridColumn FieldName="Number" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       NavigationStyle="Cell"
                       TotalSummaryPosition="Bottom" />
    </dxg:GridControl.View>
    <dxg:GridControl.TotalSummary>
        <dxg:GridSummaryItem DisplayFormat="Total empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.TotalSummary>
    <dxg:GridControl.GroupSummary>
        <dxg:GridSummaryItem DisplayFormat="Group empty cells count: {0}"
                             FieldName="Number"
                             SummaryType="Custom" />
    </dxg:GridControl.GroupSummary>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void CustomSummary(RowSummaryArgs args) {
        if(args.SummaryItem.PropertyName != "Number")
            return;
        if(args.SummaryProcess == SummaryProcess.Start) {
            args.TotalValue = 0;
        } 
        if(args.SummaryProcess == SummaryProcess.Calculate) {
            if(IsEmptyCell(args.FieldValue))
                args.TotalValue = (int)args.TotalValue + 1;
        }
    }
    bool IsEmptyCell(object fieldValue) {
        return !((int?)fieldValue).HasValue;
    }
}

Calculate custom statistics based on predefined statistics

GridControl calculates custom statistics after predefined statistics (Count, Sum, Min, and so on). As a result, you can use predefined statistical values ​​to calculate custom statistics.

1. Create custom statistics

2. Handle GridControl.CustomSummary / TreeListView.CustomSummary events

3. In the initialization phase, set the e.TotalValueReady property to true to ignore the calculation phase.

4. Use the DataControlBase.GetTotalSummaryValue method to obtain predefined statistics in the end phase.

<dxg:GridControl ...
                 CustomSummary="grid_CustomSummary">
    <dxg:GridColumn FieldName="ProductName"/>
    <dxg:GridColumn FieldName="UnitPrice"/>
    <dxg:GridColumn FieldName="Quantity"/>
    <dxg:GridControl.TotalSummary>
        <dxg:GridSummaryItem x:Name="avgPrice" FieldName="UnitPrice" SummaryType="Average"/>
        <dxg:GridSummaryItem x:Name="avgQuantity" FieldName="Quantity" SummaryType="Average"/>
        <dxg:GridSummaryItem ShowInColumn="ProductName" SummaryType="Custom" 
                             DisplayFormat="{}Average order: {0:c}"/>
    </dxg:GridControl.TotalSummary>
    <dxg:GridControl.View>
        <dxg:TableView ...
                       TotalSummaryPosition="Bottom">
        </dxg:TableView>
    </dxg:GridControl.View>
</dxg:GridControl>
private void grid_CustomSummary(object sender, DevExpress.Data.CustomSummaryEventArgs e) {
    if (e.IsTotalSummary) {
        switch (e.SummaryProcess) {
            case DevExpress.Data.CustomSummaryProcess.Start:
                e.TotalValueReady = true;
            break;
            case DevExpress.Data.CustomSummaryProcess.Finalize:
                var averagePrice = (decimal)grid.GetTotalSummaryValue(avgPrice);
                var averageQuantity = (decimal)grid.GetTotalSummaryValue(avgQuantity);
                e.TotalValue = averagePrice * averageQuantity;
            break;
        }
    }
}

You can use the e.GetGroupSummary method to obtain predefined group statistics.

Specify whether to calculate statistics

The CustomSummaryExists event or CustomSummaryExistsCommand property allows you to specify and apply statistical calculations and displays

Group statistics are calculated as follows only for the top group level:

<dxg:GridControl x:Name="grid"
                 ItemsSource="{Binding AccountList}"
                 CustomSummaryExistsCommand="{Binding CustomSummaryExistsCommand}">
                 <!-- ... -->
    <dxg:GridControl.GroupSummary>
        <dxg:GridSummaryItem FieldName="Age" SummaryType="Min"/>
        <dxg:GridSummaryItem FieldName="Age" SummaryType="Max"/>
    </dxg:GridControl.GroupSummary>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void CustomSummaryExistsCommand(RowSummaryExistsArgs args) {
        args.Exists = args.GroupPath[0].GroupLevel == 0;
    }
}

Guess you like

Origin blog.csdn.net/loongsking/article/details/134438431