officefileapi-devexpress-dot-spreadsheet-dot-conditionalformattingcollection-dot-addformulaexpressionconditionalformatting-x28-devexpress-dot-spreadsheet-dot-cellrange-system-dot-string-x29.md
Applies a conditional formatting rule that uses a formula to determine which cells to format.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
FormulaExpressionConditionalFormatting AddFormulaExpressionConditionalFormatting(
CellRange range,
string expression
)
Function AddFormulaExpressionConditionalFormatting(
range As CellRange,
expression As String
) As FormulaExpressionConditionalFormatting
| Name | Type | Description |
|---|---|---|
| range | CellRange |
A cell range to which the conditional formatting rule is applied.
| | expression | String |
A formula in the invariant culture. Start the formula 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 if necessary.
|
| Type | Description |
|---|---|
| FormulaExpressionConditionalFormatting |
The conditional formatting rule that this method creates.
|
This example demonstrates how to create the rule that uses a formula as a criterion to apply a conditional format.
To create a new conditional formatting rule represented by the FormulaExpressionConditionalFormatting object, access the collection of conditional formats from the Worksheet.ConditionalFormattings property and call the ConditionalFormattingCollection.AddFormulaExpressionConditionalFormatting method. Pass the following parameters:
Specify formatting options to be applied to cells if the condition is true , using the ISupportsFormatting.Formatting property of the FormulaExpressionConditionalFormatting object.
Note
Transparency is not supported in conditional formatting.
To remove the FormulaExpressionConditionalFormatting object, use the ConditionalFormattingCollection.Remove, ConditionalFormattingCollection.RemoveAt or ConditionalFormattingCollection.Clear methods.
// Create the rule to shade alternate rows without applying a new style.
FormulaExpressionConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range["$A$2:$G$15"], "=MOD(ROW(),2)=1");
// Specify formatting options to be applied to cells if the condition is true.
// Set the background color to light blue.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0xBC, 0xDA, 0xF7);
' Create the rule to shade alternate rows without applying a new style.
Dim cfRule As FormulaExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range("$A$2:$G$15"), "=MOD(ROW(),2)=1")
' Specify formatting options to be applied to cells if the condition is true.
' Set the background color to light blue.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &HBC, &HDA, &HF7)
The following code snippets (auto-collected from DevExpress Examples) contain references to the AddFormulaExpressionConditionalFormatting(CellRange, 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.
// Create the rule to shade alternate rows without applying a new style.
FormulaExpressionConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range["$A$2:$G$15"], "=MOD(ROW(),2)=1");
// Specify formatting options to be applied to cells if the condition is true.
// Create the rule to shade alternate rows without applying a new style.
FormulaExpressionConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range["$A$2:$G$15"], "=MOD(ROW(),2)=1");
// Specify formatting options to be applied to cells if the condition is true.
// Create the rule to shade alternate rows without applying a new style.
FormulaExpressionConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range["$A$2:$G$15"], "=MOD(ROW(),2)=1");
// Specify formatting options to be applied to cells if the condition is true.
' Create the rule to shade alternate rows without applying a new style.
Dim cfRule As FormulaExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range("$A$2:$G$15"), "=MOD(ROW(),2)=1")
' Specify formatting options to be applied to cells if the condition is true.
' Create the rule to shade alternate rows without applying a new style.
Dim cfRule As FormulaExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range("$A$2:$G$15"), "=MOD(ROW(),2)=1")
' Specify formatting options to be applied to cells if the condition is true.
' Create the rule to shade alternate rows without applying a new style.
Dim cfRule As FormulaExpressionConditionalFormatting = worksheet.ConditionalFormattings.AddFormulaExpressionConditionalFormatting(worksheet.Range("$A$2:$G$15"), "=MOD(ROW(),2)=1")
' Specify formatting options to be applied to cells if the condition is true.
See Also
ConditionalFormattingCollection Interface