windowsforms-devexpress-dot-xtraeditors-2e6f005f.md
Applies a format if cell values meet a specific expression.
Namespace : DevExpress.XtraEditors
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
public class FormatConditionRuleExpression :
FormatConditionRuleAppearanceBase,
IConditionalFilterItemProvider,
IFormatConditionRuleExpression,
IFormatConditionRuleBase
Public Class FormatConditionRuleExpression
Inherits FormatConditionRuleAppearanceBase
Implements IConditionalFilterItemProvider,
IFormatConditionRuleExpression,
IFormatConditionRuleBase
Use the FormatConditionRuleExpression.Expression property to specify a Boolean expression for a conditional formatting rule. If a cell value meets this expression, a format is applied to it.
See the following topics to learn more about the syntax of expressions:
To make it easy to create expressions, the Expression Editor can be used.
You can use the FormatConditionRuleAppearanceBase.PredefinedName property to apply one of the predefined style formats (Italic Text, Red Bold Text, Green Fill, Yellow Text with Yellow Fill, etc.), or use the FormatConditionRuleAppearanceBase.Appearance property to specify a custom appearance.
The images below demonstrate examples of applying a FormatConditionRuleExpression format.
Note
See the following topics to learn more:
This example illustrates how to apply a format to rows in a GridControl that match a specific Boolean expression.
An expression is a string that, when parsed and processed, evaluates some value. Expressions consist of column names, constants, operators, and functions. In this tutorial, a Boolean expression is used to specify criteria for the FormatConditionRuleExpression format. If the expression evaluates to true , the format is applied.
In this example, the format highlights rows that have discount prices less than or equal to 15. A discount price is evaluated using the expression: [UnitPrice] * (1 -[Discount])
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 user defined expression rule type. The format rule’s FormatRuleBase.Rule property will be set to a new FormatConditionRuleExpression object.
Set the GridFormatRule.Column property to any column (for instance, Unit Price ). Enable the GridFormatRule.ApplyToRow property to apply the format to entire rows instead of single column cells.
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 Fill with Red Text style format is selected.
Specify a string expression to which target cells should match using the Expression Editor. Click the ellipsis button for the FormatConditionRuleExpression.Expression property to invoke this editor, and enter the Boolean expression: “[UnitPrice] * (1 -[Discount]) <= 15”.
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();
FormatConditionRuleExpression formatConditionRuleExpression = new FormatConditionRuleExpression();
gridFormatRule.Column = colUnitPrice;
gridFormatRule.ApplyToRow = true;
formatConditionRuleExpression.PredefinedName = "Red Fill, Red Text";
formatConditionRuleExpression.Expression = "[UnitPrice] * (1 -[Discount]) <= 15";
gridFormatRule.Rule = formatConditionRuleExpression;
gridView1.FormatRules.Add(gridFormatRule);
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Dim gridFormatRule As New GridFormatRule()
Dim formatConditionRuleExpression As New FormatConditionRuleExpression()
gridFormatRule.Column = colUnitPrice
gridFormatRule.ApplyToRow = True
formatConditionRuleExpression.PredefinedName = "Red Fill, Red Text"
formatConditionRuleExpression.Expression = "[UnitPrice] * (1 -[Discount]) <= 15"
gridFormatRule.Rule = formatConditionRuleExpression
gridView1.FormatRules.Add(gridFormatRule)
Object FormatConditionRuleBase FormatConditionRuleAppearanceBase FormatConditionRuleExpression
See Also
FormatConditionRuleExpression Members
Expression Editor Customization