FormatConditionRuleExpression Class
Applies a format if cell values meet a specific expression.
Namespace: DevExpress.XtraEditors
Assembly: DevExpress.XtraEditors.v24.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
public class FormatConditionRuleExpression :
FormatConditionRuleAppearanceBase,
IFormatConditionRuleExpression,
IFormatConditionRuleBase
Remarks
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
- Some functions used in expression-based Excel Style Format Rules and expression-based unbound columns can only be exported to XLS(X) format in data-aware export mode. Refer to the Criteria Language Syntax topic for details on which functions can be exported to XLS(X) format.
See the following topics to learn more:
- GridControl: Conditional Formatting.
- PivotGridControl: Conditional Formatting.
- TreeList: Conditional Formatting.
Example
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.
You can also provide a custom style format using the FormatConditionRuleAppearanceBase.Appearance property.
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”.
See the Expression Editor, Expression Editor Customization and Criteria Language Syntax documents to learn more about expressions.
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);