windowsforms-17835-controls-and-libraries-data-grid-examples-conditional-formatting-how-to-highlight-column-values-that-match-a-condition.md
This tutorial illustrates how to apply conditional formatting to a Market Share column in a GridControl to highlight cells that match a specific condition. This example highlights Market Share column values that are less than 20%.
To create a new formatting rule at design time, invoke the Format Rule Collection Editor from the Grid Designer. It can also be accessed from the Properties grid by clicking the ellipsis button for the ColumnView.FormatRules property.
Invoke the Grid Designer and switch to the Style Format Rules page (in the Appearance category).
Click the Add button to create a new format rule (format rules in a GridControl are encapsulated by GridFormatRule objects).
Select the Format based on value rule type. The format rule’s FormatRuleBase.Rule property will be set to a new FormatConditionRuleValue object.
Set the GridFormatRule.Column property to the Market Share column. This column provides values to test against the formatting rule.
Choose one of the predefined style formats using the FormatConditionRuleAppearanceBase.PredefinedName property. You can do this in the Properties tab or the Rule tab. The Rule tab additionally allows you to see a preview of the selected style. In this example, the Red Bold Text style format is selected.
Specify the comparison operator by setting the FormatConditionRuleValue.Condition property. In this example, the Less operator is used for constructing the rule’s criteria.
Set the FormatConditionRuleValue.Value1 property to 0.20. This means that the format should be applied to cells with market share less than 20%.
Run the application. The image below illustrates the result.
The following code is equivalent to the design-time actions shown above.
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
GridFormatRule gridFormatRule = new GridFormatRule();
FormatConditionRuleValue formatConditionRuleValue = new FormatConditionRuleValue();
gridFormatRule.Column = colMarketShare;
formatConditionRuleValue.PredefinedName = "Red Bold Text";
formatConditionRuleValue.Condition = FormatCondition.Less;
formatConditionRuleValue.Value1 = .20;
gridFormatRule.Rule = formatConditionRuleValue;
gridView1.FormatRules.Add(gridFormatRule);
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Dim gridFormatRule As New GridFormatRule()
Dim formatConditionRuleValue As New FormatConditionRuleValue()
gridFormatRule.Column = colMarketShare
formatConditionRuleValue.PredefinedName = "Red Bold Text"
formatConditionRuleValue.Condition = FormatCondition.Less
formatConditionRuleValue.Value1 = 0.20
gridFormatRule.Rule = formatConditionRuleValue
gridView1.FormatRules.Add(gridFormatRule)
See Also