wpf-118922-controls-and-libraries-data-grid-conditional-formatting-conditional-formats-formatting-values-using-icon-sets.md
The Icon Sets conditional format allows you to use predefined or custom icon sets to format different value ranges.
The image below shows a grid column whose Profit cells contain icons corresponding to numeric values.
Create the IconSetFormatCondition class instance and specify the following settings to create a conditional format in code:
Use the FormatConditionBase.FieldName property to specify the column’s field name to which the conditional format should apply.
Formatting cells/rows are based on single column values by default. Use the FormatConditionBase.Expression property to specify a complex custom expression that returns a value based on several parameters (for example, several columns in a row).
Specify the target cells’ formatting:
Set a selective expression using the IndicatorFormatConditionBase.SelectiveExpression property if it is necessary to select data records to which the rule should apply.
Add the resulting IconSetFormatCondition instance to the TableView.FormatConditions (or TreeListView.FormatConditions) collection.
The following code sample illustrates how to define a conditional format in markup:
<dxg:TableView.FormatConditions>
<dxg:IconSetFormatCondition FieldName="Profit" PredefinedFormatName="Stars3IconSet"/>
</dxg:TableView.FormatConditions>
The code sample below illustrates how to define the same conditional format in code-behind:
var profitFormatCondition = new IconSetFormatCondition() {
FieldName = "Profit",
PredefinedFormatName = "Stars3IconSet"
};
view.FormatConditions.Add(profitFormatCondition);
Dim profitFormatCondition = New IconSetFormatCondition() With {
.FieldName = "Profit",
.PredefinedFormatName = "Stars3IconSet"
}
view.FormatConditions.Add(profitFormatCondition)
The IconSetFormatCondition chooses the icon based on the minimum and maximum values from your data source. You can use the IconSetFormatCondition.MinValue and IconSetFormatCondition.MaxValue properties to define custom minimum and maximum values for the format condition:
<dxg:TableView.FormatConditions>
<dxg:IconSetFormatCondition FieldName="SatisfactionScore"
PredefinedFormatName="Quarters5IconSet"
MinValue="0" MaxValue="1"/>
</dxg:TableView.FormatConditions>
<!-- ... -->
<dxg:GridColumn FieldName="SatisfactionScore">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings MaskType="Numeric"
Mask="p0"
MaskUseAsDisplayFormat="True"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
The IconSetFormat object allows you to specify custom icons and display rules:
<dxg:IconSetFormatCondition FieldName="Profit">
<dxg:IconSetFormatCondition.Format>
<dx:IconSetFormat ElementThresholdType="Number">
<dx:IconSetElement Threshold="-20" Icon="{dx:IconSet Name=RedToBlack4_1}"/>
<dx:IconSetElement Threshold="0" Icon="{dx:IconSet Name=Stars3_2}"/>
<dx:IconSetElement Threshold="10" Icon="{dx:IconSet Name=Stars3_1}"/>
</dx:IconSetFormat>
</dxg:IconSetFormatCondition.Format>
</dxg:IconSetFormatCondition>
Use the IconSetElement.Threshold property to specify the minimum value required to display the icon. The IconSetFormat.ElementThresholdType property specifies whether this value is a number or a percent relative to the minimum and maximum values in your data source.
Click the New Rule… in the Conditional Formatting Rules Manager.
Select the Format all cells based on their values rule type in the invoked New Formatting Rule dialog.
Select the Icon Sets format style.
Specify the Type and Value fields. These options determine the conditional values on which cells/rows formatting is based.
Specify the Min Value and Max Value fields to determine the range of values used in formatting. These fields are optional. The range is calculated automatically based on maximum and minimum values from cell values by default.
Specify the Icon Style field to define the visual appearance of the applied rule. You can set the rules to determine how to display icons.
See Also