Back to Devexpress

ConditionalFormattingCollection.AddTextConditionalFormatting(CellRange, ConditionalFormattingTextCondition, String) Method

officefileapi-devexpress-dot-spreadsheet-dot-conditionalformattingcollection-dot-addtextconditionalformatting-x28-cellrange-conditionalformattingtextcondition-string-x29.md

latest10.6 KB
Original Source

ConditionalFormattingCollection.AddTextConditionalFormatting(CellRange, ConditionalFormattingTextCondition, String) Method

Applies the conditional formatting rule that formats cells based on the text they contain.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
TextConditionalFormatting AddTextConditionalFormatting(
    CellRange range,
    ConditionalFormattingTextCondition condition,
    string text
)
vb
Function AddTextConditionalFormatting(
    range As CellRange,
    condition As ConditionalFormattingTextCondition,
    text As String
) As TextConditionalFormatting

Parameters

NameTypeDescription
rangeCellRange

A CellRange object that specifies a range of cells to which the conditional formatting rule is applied.

| | condition | ConditionalFormattingTextCondition |

One of the ConditionalFormattingTextCondition enumeration values.

| | text | String |

A String value that represents text to be used as a formatting criterion. This value can also specify a formula that returns text. If you use a formula, start it with an equal sign (=).

|

Returns

TypeDescription
TextConditionalFormatting

An IconSetConditionalFormatting object that represents the specified conditional format.

|

Example

This example demonstrates how to create a rule that applies a conditional format based on the text in a cell.

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

Note

Transparency is not supported in conditional formatting.

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

View Example

vb
' Create the rule to highlight values with the given text string in cells A2 through A15.
Dim cfRule As TextConditionalFormatting = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range("$A$2:$A$15"), ConditionalFormattingTextCondition.Contains, "Bradbury")
' Specify formatting options to be applied to cells if the condition is true.
' Set the background color to pink.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &HE1, &H95, &HC2)
csharp
// Create the rule to highlight values with the given text string in cells A2 through A15.
TextConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range["$A$2:$A$15"], ConditionalFormattingTextCondition.Contains, "Bradbury");
// Specify formatting options to be applied to cells if the condition is true.
// Set the background color to pink.
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0xE1, 0x95, 0xC2);

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddTextConditionalFormatting(CellRange, ConditionalFormattingTextCondition, 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#L130

csharp
// Create the rule to highlight values with the given text string in cells A2 through A15.
TextConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet["$A$2:$A$15"], ConditionalFormattingTextCondition.Contains, "Bradbury");
// 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#L128

csharp
// Create the rule to highlight values with the given text string in cells A2 through A15.
TextConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range["$A$2:$A$15"], ConditionalFormattingTextCondition.Contains, "Bradbury");
// 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#L127

csharp
// Create the rule to highlight values with the given text string in cells A2 through A15.
TextConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range["$A$2:$A$15"], ConditionalFormattingTextCondition.Contains, "Bradbury");
// 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#L123

vb
' Create the rule to highlight values with the given text string in cells A2 through A15.
Dim cfRule As TextConditionalFormatting = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet("$A$2:$A$15"), ConditionalFormattingTextCondition.Contains, "Bradbury")
' 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#L115

vb
' Create the rule to highlight values with the given text string in cells A2 through A15.
Dim cfRule As TextConditionalFormatting = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range("$A$2:$A$15"), ConditionalFormattingTextCondition.Contains, "Bradbury")
' 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#L113

vb
' Create the rule to highlight values with the given text string in cells A2 through A15.
Dim cfRule As TextConditionalFormatting = worksheet.ConditionalFormattings.AddTextConditionalFormatting(worksheet.Range("$A$2:$A$15"), ConditionalFormattingTextCondition.Contains, "Bradbury")
' Specify formatting options to be applied to cells if the condition is true.

See Also

ConditionalFormattingCollection Interface

ConditionalFormattingCollection Members

DevExpress.Spreadsheet Namespace