Articles in this section
Category / Section

How to set the horizontal alignment on summary columns in WPF DataGrid?

5 mins read

The appearance of Summary cells (Table summary, Group summary and Caption summary) are customized by writing styles for those cells. WPF DataGrid exposes the properties TableSummaryCellStyle, GroupSummaryCellStyle, CaptionSummaryCellStyle that customizes the appearance of the cell. Also SfDataGrid exposes the following properties to customize the appearance of summary cells conditionally.

Property

Description

TableSummaryCellStyleSelector

Used to customize the alignment of Table Summary cell

GroupSummaryCellStyleSelector

Used to customize the alignment of Group Summary cell.

CaptionSummaryCellStyleSelector

Used to customize the alignment of Caption Summary cell.



 

Now you can learn how to change the horizontal alignment of summary cells for a particular columns only using StyleSelector properties exposed in SfDataGrid. Create a class deriving from StyleSelector and override SelectStyle method (When WinRT overrides SelectStyleCore method) and return styles based on condition. In the SelectStyle override method, you can access the Cell (for eg: GridTableSummaryCell) and from cell you can get the column details and customize the alignment based on column names. Refer the following code example.

C#

public class TableSummaryStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var cell = container as GridTableSummaryCell;
        // TableSummaryStyle1 and TableSummaryStyle2 are the styles defined in Xaml.
        if (cell.ColumnBase.GridColumn.MappingName == "QS1" || cell.ColumnBase.GridColumn.MappingName == "QS2")            
            return App.Current.Resources["TableSummaryStyle1"] as Style;                        
        return App.Current.Resources["TableSummaryStyle2"] as Style;
    }
}

 

The following TableSummaryStyle1 and TableSummaryStyle2 styles are returned to the Table Summary Cells based on the column name. Refer the following code example.

XAML

<Window.Resources>
    <Style x:Key="TableSummaryStyle1" TargetType="syncfusion:GridTableSummaryCell">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="BorderBrush" Value="#FF7fd0de" />
    </Style>
    <Style x:Key="TableSummaryStyle2" TargetType="syncfusion:GridTableSummaryCell">
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
            <Setter Property="BorderBrush" Value="#FF7fd0de" />
     </Style>
  <local:TableSummaryStyleSelector x:Key="tableSummaryStyleSelector" />
</Window.Resources>
 
 

 

The following TableSummaryCellStyleSelector property is used to customize the horizontal alignment of table summary cells. Refer the following code example.

XAML

 
<syncfusion:SfDataGrid x:Name="sfDataGrid"
                                      AutoGenerateColumns="False"
                                      AllowFiltering="True"
                                      ColumnSizer="Star"
                                      ItemsSource="{Binding SalesDetails}"
                                      NavigationMode="Row" 
                                      TableSummaryCellStyleSelector="{StaticResource tableSummaryStyleSelector}" 
                                      GroupCaptionTextFormat="Sales details in {ColumnName} : {Key}">

 

Note: Group Summary cell and Caption Summary cell horizontal alignment are customized like the horizontal alignment customization of Table Summary Cell

 

Refer the following screenshot for horizontal alignment of table summary cell customization.

               

In the above screenshot, horizontal alignment is set as center for Sales in Quarter1, Sales in Quarter2 table summary cells and right for other table summary cells.

Sample Links:

WPF

WRT

UWP

 

Conclusion

I hope you enjoyed learning about how to set the horizontal alignment on summary columns in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentationand how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to present and manipulate data.

For current customers, you can check out our WPF Controls from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WPF DataGrid and other WPF controls.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied