Back to Devexpress

How to: Format Cells that are Between or Not Between Two Values

wpf-16389-controls-and-libraries-spreadsheet-examples-conditional-formatting-how-to-format-cells-that-are-between-or-not-between-two-values.md

latest3.1 KB
Original Source

How to: Format Cells that are Between or Not Between Two Values

  • Jun 07, 2019
  • 2 minutes to read

This example demonstrates how to apply the conditional formatting rule, highlighting cells that are between or not between two values.

  1. To create a new conditional formatting rule represented by the RangeConditionalFormatting object, access the collection of conditional formats from the Worksheet.ConditionalFormattings property and call the ConditionalFormattingCollection.AddRangeConditionalFormatting method. Pass the following parameters:

  2. Specify formatting options to be applied to cells if the condition is true using the ISupportsFormatting.Formatting property of the RangeConditionalFormatting object. Set the background and font colors.

To remove the RangeConditionalFormatting object, use the ConditionalFormattingCollection.Remove, ConditionalFormattingCollection.RemoveAt or ConditionalFormattingCollection.Clear methods.

View Example

vb
' Create the rule to identify values below 7 and above 19 in cells F2 through F15.  
Dim cfRule As RangeConditionalFormatting = worksheet.ConditionalFormattings.AddRangeConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingRangeCondition.Outside, "7", "19")
' Specify formatting options to be applied to cells if the condition is true.
' Set the background color to yellow.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &HFA, &HF7, &HAA)
' Set the font color to red.
cfRule.Formatting.Font.Color = Color.Red
csharp
// Create the rule to identify values below 7 and above 19 in cells F2 through F15.  
RangeConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRangeConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingRangeCondition.Outside, "7", "19");
// Specify formatting options to be applied to cells if the condition is true.
// Set the background color to yellow.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0xFA, 0xF7, 0xAA);
// Set the font color to red.
cfRule.Formatting.Font.Color = Color.Red;