Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Data Binding
   [Expand]Views
   [Expand]Data Editing and Validation
   [Expand]Grouping
   [Expand]Sorting
   [Expand]Filter and Search
   [Expand]Summaries
   [Expand]Focus and Selection Handling
    Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
    Scrolling
   [Expand]Batch Modifications
   [Expand]Hit Information
    Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Collapse]Examples
    [Collapse]Conditional Formatting
      How to: Apply a Data Bar Format to a Column
      How to: Apply an Icon Set Format to a Column
      How to: Apply a Two-Color Scale Format to a Column
      How to: Apply a Three-Color Scale Format to a Column
      How to: Highlight Column Values that Match a Condition
      How to: Highlight the Top 20% Column Values
      How to: Apply a Format Based on an Expression
    [Expand]Data Binding
    [Expand]Data Editing
    [Expand]Data Presentation
    [Expand]Export and Printing
    [Expand]Filtering
    [Expand]Formatting
    [Expand]General Use
    [Expand]Layout View
    [Expand]Master-Detail
    [Expand]Miscellaneous
    [Expand]Navigation and Selection
    [Expand]Painting
    [Expand]Skins and Look and Feel
    [Expand]Sorting and Grouping
    [Expand]Summaries
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

How to: Apply a Format Based on an Expression

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.
  1. Invoke the Grid Designer and switch to the Style Format Rules page (in the Appearance category).

  2. Click the Add button 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.

  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.

  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.

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

    See the Expression Editor, Expressions Overview and Expression Operators, Functions, and Constants documents to learn more about expressions.

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

The following code is equivalent to the design-time actions shown above.

Expanded See Also

How would you rate this topic?​​​​​​​