Articles in this section
Category / Section

How to improve performance when using formatting and styling for the cells based on data in WPF DataGrid?

6 mins read

In SfDataGrid, you can customize the Cell/Row style based on the data in the following three ways,

  1. StyleSelector
  2. DataTriggers
  3. Customizing property in Style using Converters

StyleSelector

You can customize the Cell/Row based on content by using CellStyleSelector/ RowStyleSelector, but it affects the performance while styling more number of columns or rows. Therefore, this is not a recommended way for styling the SfDataGrid for more number of columns or rows. You can use this StyleSelector according to your necessity.

To know more about the StyleSelector you can refer to the following links:

https://help.syncfusion.com/wpf/datagrid/conditional-styling#conditional-styling-of-cells-using-style-selector

https://help.syncfusion.com/wpf/datagrid/conditional-styling#conditional-styling-of-rows-using-style-selector

https://www.syncfusion.com/kb/2600/how-to-set-background-color-for-cells-in-a-column-based-on-the-cell-content

DataTriggers

You can customize the Cell/Row based on content by using Style.Triggers, but compared to Binding Converter, the performance is low when styling more number of columns or rows in SfDataGrid.

To know more about the DataTriggers you can refer to the following links:

https://www.syncfusion.com/kb/2600/how-to-set-background-color-for-cells-in-a-column-based-on-the-cell-content

Customizing the properties in Style using Converters

In SfDataGrid, customizing properties in Style using Converter is the suggested way to customize the properties of Cell/Row, based on its content. As it provides improved performance compared to other two ways.

For Cell Style

In the following code example, the Background of the GridCell for a particular column is customized based on its content using Converters.

XAML

<Window.Resources>
    <local:CellStyleConverter x:Key="cellstyleconverter"/>
</Window.Resources>
<!--Defining SfDataGrid-->
<syncfusion:SfDataGrid x:Name="datagrid"
                    ItemsSource="{Binding GDCSource}"
                    AutoGenerateColumns="True"
                    ColumnSizer="Star"/>
<!--Setting GridCell key to SfDataGrid.Columns-->
<syncfusion:SfDataGrid.Columns>
    <syncfusion:GridTextColumn MappingName="EmployeeDesignation">
        <syncfusion:GridTextColumn.CellStyle>
            <Style TargetType="syncfusion:GridCell">
                <Setter Property="Background" Value="{Binding EmployeeDesignation, Converter = {StaticResource cellstyleconverter}}" />
            </Style>
        </syncfusion:GridTextColumn.CellStyle>
    </syncfusion:GridTextColumn>
</syncfusion:SfDataGrid.Columns>

The following code example illustrates the CellStyleConverter for customizing the Cell style based on the Cell content.

C#

class CellStyleConverter:IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var cellvalue = value as string;
        if (cellvalue == "SoftwareEngineer")
            return "CadetBlue";
        else if (cellvalue == "SalesRepresentative")
            return "OrangeRed";
        else
            return "YellowGreen";
    }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The following screenshot displays the output of CellStyle in SfDataGrid.

F:\Farjana\F Drive\KB\Styles Performance\CellStyle 1.png

Figure 1: CellStyle in SfDataGrid

For Row Style

In the following code example, the Background of the VirtualizingCellsControl is customized based on its content using Converters.

XAML

<!--Customizing VirtualizingCellsControl -->
<Window.Resources>
    <local:RowStyleConverter x:Key="rowstyleconverter"/>
    <Style x:Key="stylecolor" TargetType="syncfusion:VirtualizingCellsControl">
        <Setter Property="Background" Value="{Binding Converter={StaticResource rowstyleconverter}}"/>
    </Style>
</Window.Resources>
<!--Setting VirtualizingCellsControl key to RowStyle in SfDataGrid -->
<syncfusion:SfDataGrid x:Name="datagrid"
                    RowStyle="{StaticResource stylecolor}"
                    ItemsSource="{Binding GDCSource}"
                    AutoGenerateColumns="True"
                    ColumnSizer="Star"/>

The following code example illustrates the RowStyleConverter for customizing the Cell/Row style.

C#

class RowStyleConverter:IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((value as BusinessObjects).EmployeeSalary==null)
            return "CadetBlue";
        else if((value as BusinessObjects).EmployeeName==null)
            return " OrangeRed ";
        else
            return " YellowGreen ";
    }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The following screenshot displays the output of RowStyle in SfDataGrid.

F:\Farjana\F Drive\KB\Styles Performance\RowStyle1.png

Figure 2: RowStyle in SfDataGrid

Sample Link:

Refer to the following sample links to customize the GridCell, based on the Converter to increase the performance of the SfDataGrid in WPF and Silverlight.

WPF: StylePerformanceofSfDataGrid_WPF

Silverlight: StylePerformanceofSfDataGrid_SilverLight


Conclusion

I hope you enjoyed learning about how to improve performance when using formatting and styling for the cells based on data in WPF DataGrid.


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

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

If you have any queries or require clarifications, please let us know in the comments section 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
Please sign in to leave a comment
Access denied
Access denied