Skip to main content

How to: Apply a Format Based on an Expression

  • 3 minutes to read

This example illustrates how to apply a format to rows in a GridControl that match a specific Boolean expression.

GridFormatRuleExpressionExampleResult

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.

  1. Invoke the Grid Designer and switch to the Style Format Rules page (in the Appearance category).

    GridDesignerAppearanceCategoryFormatRulesPage

  2. Click the Add button GridDesignerAddButton to create a new format rule (format rules in a GridControl are encapsulated by GridFormatRule objects).
  3. 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.

    CreateNewExpressionRuleViaGridDesigner

  4. 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.

    FormatRuleExpressionApplyToRowProperty

  5. 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.

    FormatRuleExpressionPredefinedName

    You can also provide a custom style format using the FormatConditionRuleAppearanceBase.Appearance property.

  6. 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”.

    FormatRuleExpressionCreateExpression

    See the Expression Editor, Expression Editor Customization and Criteria Language Syntax documents to learn more about expressions.

  7. Run the application. The image below illustrates the result.

    GridFormatRuleExpressionExampleResult

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);
See Also