Category / Section
How to apply different styles to the GridTableSummaryRow in WPF DataGrid?
1 min read
The SfDataGrid provides option to display the TableSummary, either at the top or bottom of the SfDataGrid by setting the GridTableSummaryRow.Position property. It is possible to apply different styles for the top and bottom table summaries by using the StyleSelector and by setting the SfDataGrid.TableSummaryRowStyleSelector property. This is demonstrated in the following code example.
<!--Adds the code example in the Resources Define with Key-->. <local:TableSummaryStyleSelector x:Key="tableSummaryStyleSelector" /> <Syncfusion:SfDataGrid x:Name="SfdataGrid" TableSummaryRowStyleSelector="{StaticResource tableSummaryStyleSelector}" ItemsSource="{Binding GDCSource}" AutoGenerateColumns="False"> <Syncfusion:SfDataGrid.TableSummaryRows> <Syncfusion:GridTableSummaryRow Position="Top" ShowSummaryInRow="False"> <Syncfusion:GridTableSummaryRow.SummaryColumns> <Syncfusion:GridSummaryColumn Name="EmployeeSalary" Format="'{Sum:c}'" MappingName="EmployeeSalary" SummaryType="Int32Aggregate" /> </Syncfusion:GridTableSummaryRow.SummaryColumns> </Syncfusion:GridTableSummaryRow> <Syncfusion:GridTableSummaryRow Position="Bottom" Title="Total EmployeeCount : {EmployeeCount}" ShowSummaryInRow="True"> <Syncfusion:GridSummaryRow.SummaryColumns> <Syncfusion:GridSummaryColumn Name="EmployeeCount" Format="'{Count:d}'" MappingName="EmployeeName" SummaryType="Int32Aggregate" /> </Syncfusion:GridSummaryRow.SummaryColumns> </Syncfusion:GridTableSummaryRow> </Syncfusion:SfDataGrid.TableSummaryRows> </Syncfusion:SfDataGrid>
Two different table summary styles are defined in the Application.Resources.
<Application.Resources> <Style x:Key="TableSummaryStyle1" TargetType="Syncfusion:TableSummaryRowControl"> <Setter Property="Foreground" Value="Red" /> </Style> <Style x:Key="TableSummaryStyle2" TargetType="Syncfusion:TableSummaryRowControl"> <Setter Property="Foreground" Value="Blue" /> </Style> </Application.Resources>
Different styles are applied to the GridTableSummaryRow based on the GridTableSummaryRow.Position.
public class TableSummaryStyleSelector : StyleSelector { public override Style SelectStyle(object item, DependencyObject container) { var rowData = (item as SpannedDataRow).RowData; var summaryRecordEntry = rowData as SummaryRecordEntry; var gridTableSummaryRow = summaryRecordEntry.SummaryRow as GridTableSummaryRow; if (gridTableSummaryRow.Position == TableSummaryRowPosition.Top) { return App.Current.Resources["TableSummaryStyle1"] as Style; } else if (gridTableSummaryRow.Position == TableSummaryRowPosition.Bottom) { return App.Current.Resources["TableSummaryStyle2"] as Style; } return null; } }
In the following screenshot, the different styles are applied to the different GridTableSummaryRows.
Sample Links: