wpf-6128-controls-and-libraries-data-grid-data-summaries-total-summary.md
The total summary is a value of an aggregate function calculated over all data rows within a View. Total summaries are displayed in the Summary Panel and Fixed Summary Panel.
The following list describes the difference between the two panels:
Set the DataViewBase.ShowFixedTotalSummary property to true to display the Fixed Summary Panel.
Use the DataViewBase.TotalSummaryPosition property to display the Summary Panel and specify its position.
TotalSummaryPosition=”Bottom”
TotalSummaryPosition=”Top”
Total summaries are the GridSummaryItem objects. The GridControl stores its total summaries in the GridControl.TotalSummary collection.
Set a summary’s Alignment property to Left or Right if you want to display the corresponding value in the Fixed Summary Panel. Otherwise, the value appears in the Summary Panel.
The TotalSummary Collection Editor allows you to create total summaries at design time. To open this editor, click the Edit button in the Visual Studio’s Properties menu:
In this editor, you can add items to the total summary collection and specify summary settings:
Follow the steps below to create a total summary in XAML:
<dxg:GridControl ...>
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem SummaryType="Count" Alignment="Left"
DisplayFormat="Total Users: {0}"/>
<dxg:GridSummaryItem FieldName="Age" SummaryType="Min"/>
<dxg:GridSummaryItem FieldName="Age" SummaryType="Max"/>
</dxg:GridControl.TotalSummary>
<dxg:GridControl.View>
<dxg:TableView TotalSummaryPosition="Bottom" ShowFixedTotalSummary="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
Note
If you use ColumnBase.Binding properties to populate columns with data, their ColumnBase.FieldName property values have the following format: RowData.Row.{Your binding path}.
Refer to the following help topic for more information: How the GridControl Identifies Columns.
using DevExpress.Data;
using DevExpress.Xpf.Grid;
// Add summaries to the GridControl's Total Summary collection.
grid.TotalSummary.AddRange(new List<GridSummaryItem>() {
// Create summary objects and specify their settings.
new GridSummaryItem() {
SummaryType = SummaryItemType.Count,
Alignment = GridSummaryItemAlignment.Left,
DisplayFormat = "Total Users: {0}"
},
new GridSummaryItem() {
FieldName = "Age",
SummaryType = SummaryItemType.Min
},
new GridSummaryItem() {
FieldName = "Age",
SummaryType = SummaryItemType.Max
}
});
Imports DevExpress.Data
Imports DevExpress.Xpf.Grid
' Add summaries to the GridControl's Total Summary collection.
grid.TotalSummary.AddRange(New List(Of GridSummaryItem)() From {
' Create summary objects and specify their settings.
New GridSummaryItem() With {
.SummaryType = SummaryItemType.Count,
.Alignment = GridSummaryItemAlignment.Left,
.DisplayFormat = "Total Users: {0}"
},
New GridSummaryItem() With {
.FieldName = "Age",
.SummaryType = SummaryItemType.Min
},
New GridSummaryItem() With {
.FieldName = "Age",
.SummaryType = SummaryItemType.Max
}
})
Use the AddRange method to add multiple summaries to the collection.
View Example: Display Total Summaries
The Summary Panel’s popup menu allows users to show, hide, and customize total summaries. Use the DataViewBase.IsTotalSummaryMenuEnabled property to enable or disable this menu.
You can use the DataViewBase.ShowTotalSummaryEditor method or the DataViewCommandsBase.ShowTotalSummaryEditor command to invoke the Summary Editor.
Refer to the following help topics for more information: Edit Summaries, Runtime Summary Editor.
The GridControl can calculate summaries against selected rows and cells. To calculate summary for selected rows/cells, set the SummaryItemBase.CalculationMode property to SelectedRows or Mixed:
<dxg:GridControl ...>
<!-- -->
<dxg:GridControl.View>
<dxg:TableView TotalSummaryPosition="Bottom"/>
</dxg:GridControl.View>
<dxg:GridControl.TotalSummary>
<!-- The summary calculated against selected rows -->
<dxg:GridSummaryItem FieldName="Total" SummaryType="Sum" CalculationMode="SelectedRows"
DisplayFormat="Selection Total=${0:N}"/>
<!-- The summary calculated against all rows -->
<dxg:GridSummaryItem FieldName="Total" SummaryType="Sum"
DisplayFormat="Total=${0:N}"/>
</dxg:GridControl.TotalSummary>
</dxg:GridControl>
Run Demo: Data Grid - Multi Row Selection
The SummaryItemBase.CalculationMode property allows you to define the following summary calculation mechanisms:
AllRowsThe summary value is calculated against all rows.SelectedRowsThe summary value is calculated against selected rows.MixedThe summary value is calculated against selected rows if their number is more than one; otherwise, against all rows.
You can also use the DataViewBase.SummaryCalculationMode property to apply the specified calculation mode to all the GridControl summaries.
The GridControl displays total summaries in the column specified by the summary item’s FieldName property. If your grid does not display this column, the summary is hidden. You can use one of the following techniques to keep the summary visible:
true and the SummaryItemBase.Alignment property to Left or Right to display this summary in the Fixed Summary Panel.<dxg:GridControl ...>
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem FieldName="Total" SummaryType="Sum" ShowInColumn="ProductName"/>
<dxg:GridSummaryItem FieldName="Total" SummaryType="Sum" Alignment="Right"/>
</dxg:GridControl.TotalSummary>
<dxg:GridControl.View>
<dxg:TableView TotalSummaryPosition="Bottom" ShowFixedTotalSummary="True"/>
</dxg:GridControl.View>
<!-- ... -->
<dxg:GridColumn FieldName="Total"
UnboundDataType="{x:Type sys:Decimal}"
UnboundExpression="[UnitPrice]*[Quantity]"
Visible="False">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings DisplayFormat="$0.00"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</dxg:GridControl>
In Master-Detail mode, you can calculate summaries for a detail GridControl. To show summaries at the bottom of the detail grid, set its DataViewBase.TotalSummaryPosition property to Bottom or the DataViewBase.ShowFixedTotalSummary property to true. In this case, total summaries are only visible when you scroll to the bottom of the detail.
Set the detail view’s TotalSummaryPosition property to Top to always display the Total Summary Panel regardless of the vertical scrolling position.
The following table lists properties that allow you to check the summary existence or obtain total summary objects, their values and text:
| API | Description |
|---|---|
| GridControl.TotalSummary / TreeListControlBase.TotalSummary | Allows you to access the collection of total summary items. |
| DataControlBase.GetTotalSummaryValue | Returns the value of the specified total summary item. |
| ColumnBase.HasTotalSummaries | Gets whether the column displays total summaries. This is a dependency property. |
| ColumnBase.TotalSummaries | Gets the list of total summary items displayed within this column. This is a dependency property. |
| ColumnBase.TotalSummaryText | Gets the text displayed in the summary panel‘s cell. This is a dependency property. |
| DataViewBase.FixedSummariesRight | Gets total summaries displayed within the Fixed Summary Panel and aligned to the right. |
| DataViewBase.FixedSummariesLeft | Gets total summaries displayed within the Fixed Summary Panel and aligned to the left. |
The GridControl updates its summaries after you post an edited row’s changes to a data source. Call the DataViewBase.CommitEditing method in the GridViewBase.CellValueChanged event handler to update summaries each time a user edits a cell value:
void view_CellValueChanged(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e) {
view.CommitEditing();
}
Private Sub view_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.Xpf.Grid.CellValueChangedEventArgs)
view.CommitEditing()
End Sub
The GridControl does not update its summaries if you modify the control’s data source directly (bypassing the control’s UI). Set the DataControlBase.AllowLiveDataShaping property to true to make the control recalculate summaries when the data source changes.
You can also call the DataControlBase.UpdateTotalSummary method to recalculate summary values.
The GridControl can use an optimized summary recalculation mechanism, which processes only changed data records. As a result, the summary update time does not depend on the number of records.
Set the GridControl.OptimizeSummaryCalculation property to true to enable the optimized summary recalculation. The DevExpress.Xpf.Grid.GridControl should be bound to an ObservableCollection<T> or ChunkList<T> whose items implement the INotifyPropertyChanged and INotifyPropertyChanging interfaces.
The GridControl cannot optimize the recalculation of Custom Summaries and summaries for Unbound Columns.
The following table lists properties that allow you to customize total summaries:
| Property | Description |
|---|---|
| TotalSummaryElementStyle | Gets or sets the style applied to individual text elements in the total summary items within a view. This is a dependency property. |
| FixedTotalSummaryElementStyle | Gets or sets the style applied to individual text elements in the fixed total summary item. This is a dependency property. |
| TotalSummaryContentStyle | Gets or sets the style applied to total summary items displayed within a View. This is a dependency property. |
| TotalSummaryItemTemplate | Gets or sets the template that defines the presentation of total summary items. This is a dependency property. |
| TotalSummaryItemTemplateSelector | Gets or sets an object that chooses a total summary template based on custom logic. This is a dependency property. |
The code sample below changes the appearance of total summaries based on their values. This code sample uses the DXBinding mechanism to simplify the trigger definition.
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem FieldName="UnitPrice" SummaryType="Max"/>
<dxg:GridSummaryItem FieldName="UnitPrice" SummaryType="Min"/>
<dxg:GridSummaryItem FieldName="Quantity" SummaryType="Max"/>
<dxg:GridSummaryItem FieldName="Quantity" SummaryType="Min"/>
</dxg:GridControl.TotalSummary>
<dxg:GridControl.View>
<dxg:TableView TotalSummaryPosition="Bottom">
<dxg:TableView.TotalSummaryElementStyle>
<Style TargetType="Run">
<Setter Property="FontWeight" Value="Bold"/>
<Style.Triggers>
<DataTrigger Binding="{DXBinding '(double)Value le 15'}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{DXBinding '(double)Value gt 15'}" Value="True">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.TotalSummaryElementStyle>
</dxg:TableView>
</dxg:GridControl.View>
See Also