Back to Devexpress

How to: Apply a Two-Color Scale Format to a Column

windowsforms-17832-controls-and-libraries-data-grid-examples-conditional-formatting-how-to-apply-a-two-color-scale-format-to-a-column.md

latest3.8 KB
Original Source

How to: Apply a Two-Color Scale Format to a Column

  • Nov 13, 2018
  • 3 minutes to read

This example illustrates how to apply a two-color scale format to the Unit Price column in a GridControl at design time using the Grid Designer and in code.

A two-color scale reflects data distribution of column cell values using a gradient of two colors. In this example, the predefined White - Red color scale is used. Cells with lower values will have a more white color and higher value cells will have a more red color.

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 using 2 color scales rule type. The format rule’s FormatRuleBase.Rule property will be set to a new FormatConditionRule2ColorScale object.

  4. Set the GridFormatRule.Column property to the Unit Price column. This column provides values to test against the formatting rule.

  5. Choose one of the predefined colors scales using the FormatConditionRule2ColorScale.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 color scale. In this example, the White - Red color scale is selected.

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

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

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;

GridFormatRule gridFormatRule = new GridFormatRule();
FormatConditionRule2ColorScale formatConditionRule2ColorScale = new FormatConditionRule2ColorScale();
gridFormatRule.Column = colUnitPrice;
formatConditionRule2ColorScale.PredefinedName = "White, Red";
gridFormatRule.Rule = formatConditionRule2ColorScale;
gridView1.FormatRules.Add(gridFormatRule);
vb
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid

Dim gridFormatRule As New GridFormatRule()
Dim formatConditionRule2ColorScale As New FormatConditionRule2ColorScale()
gridFormatRule.Column = colUnitPrice
formatConditionRule2ColorScale.PredefinedName = "White, Red"
gridFormatRule.Rule = formatConditionRule2ColorScale
gridView1.FormatRules.Add(gridFormatRule)

See Also

Appearance and Conditional Formatting

Grid Designer

Style Format Rules Page