Skip to main content

How to: Highlight the Top 20% Column Values

  • 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).

GridFormatRuleTopBottomExampleResult

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

    CreateNewTopBottomRuleViaGridDesigner

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

    SelectColumnForTopBottomFormatRule

    By default, the format is applied to the same column. However, you can apply this format to another column by setting the GridFormatRule.ColumnApplyTo property. In addition, you can apply the format to the entire row by setting the GridFormatRule.ApplyToRow property to true.

  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.

    FormatRuleTopBottomPredefinedName

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

  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.

    FormatRuleTopBottomExampleSettings

  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.

    GridFormatRuleTopBottomExampleResult

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

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