Skip to main content

Creating Conditional Formatting Rules

  • 2 minutes to read

You can create conditional formatting rules at design time (in markup or with the ASPxGridView Designer) or at runtime.

Grid_ConditionalFormatting

Format conditions are GridFormatConditionBase class descendants. Create a class instance of the following descendants and add it to the ASPxGridView.FormatConditions collection.

See the following topic for more information: Format Condition Types.

Note

Use the GridViewFormatConditionHighlight.ApplyToRow and GridViewFormatConditionTopBottom.ApplyToRow properties to apply specified format rules to a row.

Creating Conditional Formatting Rules in Markup

<dx:ASPxGridView runat="server" ... >
        <Columns>...</Columns>
     <FormatConditions>
          <dx:GridViewFormatConditionTopBottom FieldName="UnitPrice" Rule="AboveAverage" Format="RedText" />
          <dx:GridViewFormatConditionHighlight FieldName="Discount" Expression="[Discount] > 0" Format="GreenFillWithDarkGreenText" />
          <dx:GridViewFormatConditionColorScale FieldName="Quantity" Format="BlueWhiteRed" />
          <dx:GridViewFormatConditionIconSet FieldName="Total" Format="Ratings5" />
     </FormatConditions>
</dx:ASPxGridView>

Creating Conditional Formatting Rules with the Grid Designer

In the ASPxGridView Designer, select Feature Browser and then choose Conditional Formatting to create and customize conditional formatting rules.

ConditionslFormatting_Designer

Creating Conditional Formatting Rules at Runtime

GridViewFormatConditionTopBottom Rule1 = new GridViewFormatConditionTopBottom();
Rule1.FieldName = "UnitPrice";
Rule1.Rule = GridTopBottomRule.AboveAverage;
Rule1.Format = GridConditionHighlightFormat.RedText;
Grid.FormatConditions.Add(Rule1);

GridViewFormatConditionHighlight Rule2 = new GridViewFormatConditionHighlight();
Rule2.FieldName = "Discount";
Rule2.Expression = "[Discount] > 0";
Rule2.Format = GridConditionHighlightFormat.GreenFillWithDarkGreenText;
Grid.FormatConditions.Add(Rule2);

GridViewFormatConditionHighlight Rule2_1 = new GridViewFormatConditionHighlight();
Rule2_1.CellStyle.BackColor = Color.Red;
Rule2_1.Format = GridConditionHighlightFormat.Custom;
Rule2_1.Rule = GridConditionRule.Expression;
Rule2_1.Expression = "[C] = 'Name 1'";
Rule2_1.FieldName = "C";
ASPxGridView1.FormatConditions.Add(Rule2_1);

GridViewFormatConditionHighlight Rule2_2 = new GridViewFormatConditionHighlight();
Rule2_2.CellStyle.BackColor = Color.Red;
Rule2_2.Format = GridConditionHighlightFormat.Custom;
Rule2_2.Rule = GridConditionRule.Equal;
Rule2_2.Value1 = DateTime.Today.AddDays(2);
Rule2_2.FieldName = "B";
ASPxGridView1.FormatConditions.Add(Rule2_2);

GridViewFormatConditionColorScale Rule3 = new GridViewFormatConditionColorScale();
Rule3.FieldName = "Quantity";
Rule3.Format = GridConditionColorScaleFormat.BlueWhiteRed;
Grid.FormatConditions.Add(Rule3);

GridViewFormatConditionIconSet Rule4 = new GridViewFormatConditionIconSet();
Rule4.FieldName = "Total";
Rule4.Format = GridConditionIconSetFormat.Ratings5;
Grid.FormatConditions.Add(Rule4);