How to: Format Cells that Contain Dates
- 2 min to read
This example demonstrates how to apply a "date occurring..." conditional formatting rule.
To create a new conditional formatting rule represented by the TimePeriodConditionalFormatting object, access the collection of conditional formats from the Worksheet.ConditionalFormattings property and call the ConditionalFormattingCollection.AddTimePeriodConditionalFormatting 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 TimePeriodConditionalFormatting object.
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-apply-conditional-formatting-to-a-range-of-cells-e4959
// Create the rule to highlight today's dates in cells B2 through B6. TimePeriodConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTimePeriodConditionalFormatting(worksheet["$B$2:$B$6"], ConditionalFormattingTimePeriod.Today); // 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, 0xF2, 0xAE, 0xE3);
The image below shows the result (the workbook is opened in Microsoft® Excel®). Today's date in the DUE DATE column is highlighted in pink.