Back to Devexpress

How to: Highlight the Top 20% Column Values

windowsforms-17863-controls-and-libraries-data-grid-examples-conditional-formatting-how-to-highlight-top-column-values.md

latest4.8 KB
Original Source

How to: Highlight the Top 20% Column Values

  • Feb 18, 2022
  • 3 minutes to read

This example illustrates how to apply a top/bottom format to the Sales vs Target column in a GridControl at design time using the Grid Designer and in code.

The top/bottom format allows you to find highest or lowest cell values. In this tutorial, the top 20% values in the Sales vs Target column are highlighted (the cutoff value is 20 ).

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 only top or bottom ranked values rule type. The format rule’s FormatRuleBase.Rule property will be set to a new FormatConditionRuleTopBottom object.

  4. Set the GridFormatRule.Column property to the Sales vs Target column. This column provides values to test against the formatting rule.

  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 Green Fill with Green Text style format is selected.

  6. Set the cutoff value to 20 using the FormatConditionRuleTopBottom.Rank property.

  7. Choose the Percent rank type (the FormatConditionRuleTopBottom.RankType property) to regard the cutoff value as a percentage.

  8. Set the FormatConditionRuleTopBottom.TopBottom property to Top to highlight the highest column values.

  9. Run the application. The image below illustrates the result. The top 20% values in the Sales vs Target column are highlighted using a light green background and dark green foreground color.

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

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;

GridFormatRule gridFormatRule = new GridFormatRule();
FormatConditionRuleTopBottom formatConditionRuleTopBottom = new FormatConditionRuleTopBottom();
gridFormatRule.Column = colSalesVsTarget;
formatConditionRuleTopBottom.PredefinedName = "Green Fill, Green Text";
formatConditionRuleTopBottom.Rank = 20;
formatConditionRuleTopBottom.RankType = FormatConditionValueType.Percent;
formatConditionRuleTopBottom.TopBottom = FormatConditionTopBottomType.Top;
gridFormatRule.Rule = formatConditionRuleTopBottom;
gridView1.FormatRules.Add(gridFormatRule);
vb
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid

Dim gridFormatRule As New GridFormatRule()
Dim formatConditionRuleTopBottom As New FormatConditionRuleTopBottom()
gridFormatRule.Column = colSalesVsTarget
formatConditionRuleTopBottom.PredefinedName = "Green Fill, Green Text"
formatConditionRuleTopBottom.Rank = 20
formatConditionRuleTopBottom.RankType = FormatConditionValueType.Percent
formatConditionRuleTopBottom.TopBottom = FormatConditionTopBottomType.Top
gridFormatRule.Rule = formatConditionRuleTopBottom
gridView1.FormatRules.Add(gridFormatRule)

See Also

Appearance and Conditional Formatting

Grid Designer

Style Format Rules Page