How to: Apply a Format Based on an Expression
- 2 minutes to read
This example illustrates how to apply a format to rows in a GridControl that match a specific 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 example, a Boolean expression is used. If the expression evaluates to true, the format is applied. The format highlights rows that have values in the Total column that are less than 100.
- Create a new FormatCondition class instance.
- Set this object's FormatConditionBase.FieldName property to Total. This column provides values to test against the formatting rule.
- Assign the expression string to the ExpressionConditionBase.Expression property.
- Assign one of the predefined formats to the FormatConditionBase.PredefinedFormatName property. All available format names are listed in the Predefined Format Names document.
- Set the ExpressionConditionBase.ApplyToRow property to true to apply the format to entire rows instead of individual column cells.
- To apply a conditional formatting rule represented by the created FormatCondition object, add this object to the GridControl.FormatConditions collection.
using DevExpress.Mobile.DataGrid; // ... FormatCondition condition = new FormatCondition(); condition.FieldName = "Total"; condition.Expression = "[Total] < 100"; condition.PredefinedFormatName = "LightRedFillWithDarkRedText"; condition.ApplyToRow = true; grid.FormatConditions.Add (condition);