How to apply global styles for All SfDataGrid GridNumericColumns?

Hi Syncfusion team,

I'm trying to apply a global style to all GridNumericColumn instances in my SfDataGrid. I'd like to customize the styles for all numeric columns consistently throughout my application.

So can someone show me how please !?


2 Replies

WJ Wartol Joham October 26, 2024 06:48 AM UTC

To apply global styles to all GridNumericColumn elements in a SfDataGrid in Syncfusion, you can define a style in App.xaml or in a resource dictionary. This style will then apply globally across all GridNumericColumn columns within your application.

Here’s how to define the style:

  1. Define a Cell Style for GridNumericColumn Cells: Each GridNumericColumn cell uses a GridCellNumericRenderer, so you need to target the specific cell type.
  2. Define the style in App.xaml:

Here’s an example:

xml
<Application.Resources> <ResourceDictionary> <!-- Define the style for GridNumericColumn cells --> <Style TargetType="syncfusion:GridCellNumericRenderer"> <Setter Property="Foreground" Value="DarkBlue"/> <Setter Property="Background" Value="LightYellow"/> <Setter Property="FontSize" Value="14"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="Padding" Value="5"/> </Style> </ResourceDictionary> </Application.Resources>
  1. Applying the Style in Code-Behind (optional): You can also apply the cell style directly in the code-behind by setting the CellStyle property for each GridNumericColumn, though defining it in App.xaml provides a cleaner and more centralized solution.

Additional Properties for Numeric Columns

If you want additional styling or properties specifically for numeric formatting (like number formatting or alignment), you may add these properties directly to GridNumericColumn columns in your XAML or apply them programmatically in the code-behind.

Example of GridNumericColumn Usage in XAML

xml
<syncfusion:SfDataGrid x:Name="dataGrid" AutoGenerateColumns="False"> <syncfusion:SfDataGrid.Columns> <syncfusion:GridNumericColumn MappingName="Price" HeaderText="Price" /> <syncfusion:GridNumericColumn MappingName="Quantity" HeaderText="Quantity" /> <!-- Other columns --> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid>

With this setup, your specified styles in App.xaml will be applied globally to all GridNumericColumn cells in SfDataGrid. This centralizes your styling configuration, making it easy to maintain consistent visual styling across your application.



MS Malini Selvarasu Syncfusion Team October 28, 2024 01:27 PM UTC

Hi Joseph,

Based on the information provided, we understand that you need to implement a global style for the `GridNumericColumn`.
For Manually Defined Columns
If you are defining the columns manually, you will need to set the `CellStyle` for the `GridNumericColumn` using the appropriate key. Please refer to the code snippets below for guidance.
Code Snippet:
<Window.Resources>
<Style x:Key="numericCellStyle" TargetType="syncfusion:GridCell">
<Setter Property="Background" Value="Pink"/>
</Style>
</Window.Resources>
<syncfusion:SfDataGrid x:Name="sfDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Orders}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridNumericColumn MappingName="OrderID" CellStyle="{StaticResource numericCellStyle}"/>
<syncfusion:GridTextColumn MappingName="CustomerID" />
<syncfusion:GridTextColumn MappingName="CustomerName"/>
<syncfusion:GridTextColumn MappingName="Country"/>
<syncfusion:GridNumericColumn MappingName="UnitPrice" CellStyle="{StaticResource numericCellStyle}"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

For Auto-Generated Columns
 In cases where the columns are generated automatically, you can apply a common style for the `GridNumericColumn` during the `AutoGeneratingColumn` event. Within this event, you can determine whether the column is a `GridNumericColumn`, and if so, apply the desired style. Please see the code snippet below for reference.
 Code Snippets:
sfDataGrid.AutoGeneratingColumn += SfDataGrid_AutoGeneratingColumn;
private void SfDataGrid_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingColumnArgs e)
  {
      if (e.Column is GridNumericColumn)
      {
          e.Column.CellStyle = App.Current.MainWindow.Resources["numericCellStyle"] as Style;
      }
  }

To further assist you, we have prepared a sample that demonstrates this approach. We encourage you to review the sample below and let us know if it aligns with your requirements.
If we have misunderstood your request or if your needs differ, please provide additional details so we can gain a clearer understanding and offer the best solution.
Thank you for your cooperation and understanding.
Regards,
Malini Selvarasu

 


Attachment: SfDataGrid_Demo_a06fc8af.zip

Loader.
Up arrow icon