Working with Conditional Formatting
Conditional formatting with Crystal Reports provides an effective method for highlighting data within the report. As you moved through the Report Designer, you may have noticed that a number of formatting options had the X+2 icon shown beside them. You can see one in the following dialog box, which is accessible by right-clicking on a textbox in the report, and selecting Format:

This indicates that these formatting properties can be used with conditional formatting: when some condition is True, the formatting property will be applied.
Understanding Conditional Formatting
There are two different kinds of conditional formatting available within Crystal Reports, based on the two different kinds of properties available for Crystal Reports elements. The first deals with Boolean properties, which include things like Suppress and Can Grow, and have an "on/off" state. The second deals with properties that can have multiple outcomes, like font or section colors. While the concepts behind using these two different types of properties are similar, the formulas behind them are different.
Conditional Formatting for Boolean Properties
With these properties, there is either a True or False status - if a field has the Suppress property checked, the property is True, and it will be suppressed. If not, the property is False, and the field is shown. When working with conditional formatting on these types of properties, all we need to do is specify a condition. If that condition is True, then the property will be set to True.
For the following walkthrough of conditional formatting, open the Customer Sales report (customer_sales. rpt) that's included with the sample files for this chapter.
In the case of Suppress, you can right-click on a field (in this case, the Last Year's Sales field (not the field in the report header, but the one in the report proper!), and select Format. On the Common property page, you will see the Suppress property checkbox. To start with, don't check the box - that would turn suppression on for every value. Instead, click the X+2 box that appears to the right of the property (shown opposite) to open the formula editor, and enter a conditional formatting formula.

Since this is a Boolean-type property, all we need to do is enter a condition. When the condition is True, the formatting option will occur (in this case, the field will be suppressed). The formula would look like this:
{Customer.Last Year's Sales} < 10000
This would cause our report to show only the values that were greater than 10,000.

This conditional formatting formula was applied to an individual field, so only the field is suppressed - but you could apply it to an entire section. Be warned, though, that when you suppress a field or a section, it only suppresses the display of the field. It will still be included in any totals or formulas.
Conditional Formatting for Multiple-Outcome Properties
With properties that can have a multiple outcome, the conditional formatting formula needs to be a bit different. In our Boolean example above, only two things could happen: either the field was suppressed, or it wasn't. With multiple-outcome formulas dependent on the setting of a property, any number of things could happen. Using the same condition as above, if our value was over 10,000, we could have changed the field change color - but to which color?When working with multiple-outcome properties, we have to use an If...Then statement to specify what will happen when the condition is True. For example:
If {Customer.Last Year's Sales} > 20000 Then Red
To apply this conditional formatting, right-click again on the Last Year's Sales field again, select Format, and then switch to the property tab for Font and click the X+2 icon beside the font color. This will open the Formula Editor and allow you to enter your conditional formatting formula.In the Formula Editor, all of the available values or constants for a particular property will appear in the functions list.In addition to the constants (with a specific set for each attribute), you can also set the property to its CurrentFieldValue, or to its DefaultAttribute. For example, if you had a field in which the color property had already been set to Green, you could use this default in your formula:
If {Customer.Last Year's Sales} > 10000 Then Purple Else DefaultAttribute
With conditional formatting on multiple-outcome properties, you are not confined to a single If...Then statement. You can also used a compound If...Then, or an If...Then...Else statement, as shown below:
If {Customer.Last Year's Sales} > 100000 Then Blue
Else If {Customer.Last Year's Sales} > 50000 Then Orange _
Else If {Customer.Last Year's Sales} > 20000 Then Red
Remember that the same rules apply to these formulas as to the formulas you create to display on your report. Once the condition has been met, Crystal Reports will stop evaluating the rest of the formula.