Back to Devexpress

ConditionalFormattingCollection.AddRankConditionalFormatting(CellRange, ConditionalFormattingRankCondition, Int32) Method

officefileapi-devexpress-dot-spreadsheet-dot-conditionalformattingcollection-dot-addrankconditionalformatting-x28-cellrange-conditionalformattingrankcondition-int32-x29.md

latest10.6 KB
Original Source

ConditionalFormattingCollection.AddRankConditionalFormatting(CellRange, ConditionalFormattingRankCondition, Int32) Method

Applies the "top/bottom N" conditional formatting rule.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
RankConditionalFormatting AddRankConditionalFormatting(
    CellRange range,
    ConditionalFormattingRankCondition condition,
    int rank
)
vb
Function AddRankConditionalFormatting(
    range As CellRange,
    condition As ConditionalFormattingRankCondition,
    rank As Integer
) As RankConditionalFormatting

Parameters

NameTypeDescription
rangeCellRange

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

| | condition | ConditionalFormattingRankCondition |

One of the ConditionalFormattingRankCondition enumeration values that represent top/bottom rules.

| | rank | Int32 |

A positive integer that is a number or percentage of the rank value to be set in the conditional formatting rule.

|

Returns

TypeDescription
RankConditionalFormatting

A RankConditionalFormatting object that represents the specified conditional format.

|

Example

This example demonstrates how to apply a "top/bottom N" conditional formatting rule.

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

Note

Transparency is not supported in conditional formatting.

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

View Example

csharp
// Create the rule to identify top three values in cells F2 through F15.
RankConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingRankCondition.TopByRank, 3);
// Specify formatting options to be applied to cells if the condition is true.
// Set the background color to dark orchid.
cfRule.Formatting.Fill.BackgroundColor = Color.DarkOrchid;
// Set the outline borders.
cfRule.Formatting.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);
// Set the font color to white.
cfRule.Formatting.Font.Color = Color.White;
vb
' Create the rule to identify top three values in cells F2 through F15.
Dim cfRule As RankConditionalFormatting = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingRankCondition.TopByRank, 3)
' Specify formatting options to be applied to cells if the condition is true.
' Set the background color to dark orchid.
cfRule.Formatting.Fill.BackgroundColor = Color.DarkOrchid
' Set the outline borders.
cfRule.Formatting.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin)
' Set the font color to white.
cfRule.Formatting.Font.Color = Color.White

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddRankConditionalFormatting(CellRange, ConditionalFormattingRankCondition, Int32) 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#L101

csharp
// Create the rule to identify top three values in cells F2 through F15.
RankConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet["$F$2:$F$15"], ConditionalFormattingRankCondition.TopByRank, 3);
// 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#L99

csharp
// Create the rule to identify top three values in cells F2 through F15.
RankConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingRankCondition.TopByRank, 3);
// 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#L98

csharp
// Create the rule to identify top three values in cells F2 through F15.
RankConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range["$F$2:$F$15"], ConditionalFormattingRankCondition.TopByRank, 3);
// 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#L98

vb
' Create the rule to identify top three values in cells F2 through F15.
Dim cfRule As RankConditionalFormatting = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet("$F$2:$F$15"), ConditionalFormattingRankCondition.TopByRank, 3)
' 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#L90

vb
' Create the rule to identify top three values in cells F2 through F15.
Dim cfRule As RankConditionalFormatting = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingRankCondition.TopByRank, 3)
' 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#L88

vb
' Create the rule to identify top three values in cells F2 through F15.
Dim cfRule As RankConditionalFormatting = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range("$F$2:$F$15"), ConditionalFormattingRankCondition.TopByRank, 3)
' Specify formatting options to be applied to cells if the condition is true.

See Also

ConditionalFormattingCollection Interface

ConditionalFormattingCollection Members

DevExpress.Spreadsheet Namespace