Creating Conditional Formatting Rules

  • 2 minutes to read

Conditional formatting rules can be created at design time (in markup or using ASPxGridView Designer) or at runtime.

Grid_ConditionalFormatting

To add conditions, use the ASPxGridView.FormatConditions property, which provides access to a collection of rules represented by the following GridFormatConditionBase class descendants.

For more information, see the Format Condition Types topic.

NOTE

Use the GridViewFormatConditionHighlight.ApplyToRow and GridViewFormatConditionTopBottom.ApplyToRow properties to apply specified formatting rules to a whole 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 Using Grid Designer

To create and customize conditional formatting rules using the ASPxGridView Designer, select the Feature Browser and then Conditional Formatting.

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