Back to Devexpress

ConditionalFormattingCollection.AddExpressionConditionalFormatting(CellRange, ConditionalFormattingExpressionCondition, String) Method

officefileapi-devexpress-dot-spreadsheet-dot-conditionalformattingcollection-dot-addexpressionconditionalformatting-x28-cellrange-conditionalformattingexpressioncondition-string-x29.md

latest10.8 KB
Original Source

ConditionalFormattingCollection.AddExpressionConditionalFormatting(CellRange, ConditionalFormattingExpressionCondition, String) Method

Applies a conditional formatting rule that uses a relational operator to determine which cells to format.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
ExpressionConditionalFormatting AddExpressionConditionalFormatting(
    CellRange range,
    ConditionalFormattingExpressionCondition condition,
    string value
)
vb
Function AddExpressionConditionalFormatting(
    range As CellRange,
    condition As ConditionalFormattingExpressionCondition,
    value As String
) As ExpressionConditionalFormatting

Parameters

NameTypeDescription
rangeCellRange

A cell range to which the conditional formatting rule is applied.

| | condition | ConditionalFormattingExpressionCondition |

An enumeration value that specifies the relational operator.

| | value | String |

A threshold. You can specify a formula in the invariant culture to evaluate the threshold. If you use a formula, start it with an equal sign (=). The formula can include the Spreadsheet’s built-in functions. You can combine relative and absolute references to adjust the formula in each cell.

|

Returns

TypeDescription
ExpressionConditionalFormatting

The conditional formatting rule that this method creates.

|

Example

This example demonstrates how to create the rule that uses a relational operator as a formatting criteria.

  1. To create a new conditional formatting rule represented by the ExpressionConditionalFormatting object, access the collection of conditional formats from the Worksheet.ConditionalFormattings property and call the ConditionalFormattingCollection.AddExpressionConditionalFormatting 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 ExpressionConditionalFormatting object. Set the background and font colors.

Note

Transparency is not supported in conditional formatting.

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

View Example

csharp
// Create the rule to identify values that are above the average in cells F2 through F15.
ExpressionConditionalFormatting cfRule =
worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)");
// 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;
vb
' Create the rule to identify values that are above the average in cells F2 through F15.
Dim cfRule As ExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)")
' 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

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddExpressionConditionalFormatting(CellRange, ConditionalFormattingExpressionCondition, String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-apply-conditional-formatting-to-cell-range/CS/ConditionalFormatting_Example/SpreadsheetActions/ConditionalFormatting.cs#L210

csharp
ExpressionConditionalFormatting cfRule =
worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet["$F$2:$F$15"], ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)");
// Specify formatting options to be applied to cells if the condition is true.

winforms-spreadsheet-apply-conditional-formatting-to-a-range-of-cells/CS/SpreadsheetControl/SpreadsheetActions/ConditionalFormatting.cs#L208

csharp
ExpressionConditionalFormatting cfRule =
worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)");
// Specify formatting options to be applied to cells if the condition is true.

wpf-spreadsheet-how-to-apply-conditional-formatting-to-a-range-of-cells/CS/ConditionalFormatting_WPF_Examples/SpreadsheetActions/ConditionalFormatting.cs#L207

csharp
ExpressionConditionalFormatting cfRule =
worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)");
// Specify formatting options to be applied to cells if the condition is true.

spreadsheet-document-api-apply-conditional-formatting-to-cell-range/VB/ConditionalFormatting_Example/SpreadsheetActions/ConditionalFormatting.vb#L186

vb
' Create the rule to identify values that are above the average in cells F2 through F15.
Dim cfRule As ExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet("$F$2:$F$15"), ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)")
' Specify formatting options to be applied to cells if the condition is true.

winforms-spreadsheet-apply-conditional-formatting-to-a-range-of-cells/VB/SpreadsheetControl/SpreadsheetActions/ConditionalFormatting.vb#L181

vb
' Create the rule to identify values that are above the average in cells F2 through F15.
Dim cfRule As ExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)")
' Specify formatting options to be applied to cells if the condition is true.

wpf-spreadsheet-how-to-apply-conditional-formatting-to-a-range-of-cells/VB/ConditionalFormatting_WPF_Examples/SpreadsheetActions/ConditionalFormatting.vb#L179

vb
' Create the rule to identify values that are above the average in cells F2 through F15.
Dim cfRule As ExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddExpressionConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingExpressionCondition.GreaterThan, "=AVERAGE($F$2:$F$15)")
' Specify formatting options to be applied to cells if the condition is true.

See Also

ConditionalFormattingCollection Interface

ConditionalFormattingCollection Members

DevExpress.Spreadsheet Namespace