SfDataGrid displays all the column values in cells. SfDataGrid control allows you to apply style for these cells in a particular column in two ways.
Using this property you can customize the background of the cells in a particular column based on its cell content with the help of a Converter or by using Style.Triggers. The following code example explains how to apply background for cells in a particular column based on its cell content using GridColumn.CellStyle property with the help of a converter. Here in the NameToColor converter class, the value produced by the binding source for each cell is available in the value parameter of the Convert method and the target property for which the style is to be set is obtained from the targetType parameter. Using the value parameter, you can return a converted value to the target property based on which the background is changed. XAML
C#
The following screenshot displays the result of the above code in SfDataGrid. Sample Links: Using Style.Triggers You can also use DataTriggers to set the background of a cell based on its content. They apply property values or perform actions when the bound data meets a specified condition. It is not necessary to write a converter when you use DataTriggers that reduces the code size. You can use MultiDataTriggers for specifying a set of conditions that is satisfied and applies the property values or performs actions. The following code example explains the usage of DataTriggers to set the background of a cell based on its cell content. XAML
Here in the above code background is applied only for Column4, when the cell content is Red, the background is changed as Red using the DataTriggers. On the other hand using MultiDataTriggers, a set of conditions are given, i.e. when the value in Column4 is Yellow and value in Column3 is Cyan, then only the Background of the cell containing the text Yellow will be changed to yellow. In other cases, default background is displayed. The following screenshot displays the result of the above code in SfDataGrid. Sample Link:
You can customize the background of the cells in a particular column based on its cell content using the CellStyleSelector property. This property is used to apply style for a cell and to change the background of the GridCell. CellStyle gives you better performance when compared to CellStyleSelector. The following code example explains how to apply the background for the cells in a particular column using CellStyleSelector. XAML C#
The following screenshot displays the result of the above code in SfDataGrid.
Sample Links:
|
This page will automatically be redirected to the sign-in page in 10 seconds.