November 14, 2011

Alternating Row Colors in Crystal Reports

Creating a report to show your data in Crystal Reports is easy. It's also easy to create an uninteresting and difficult-to-read report.

This is a quick post, showing how to create striped rows that increase the readability of a report, and a few other points along the way.



I'm Using
  • Crystal Reports for Enterprise (CRE) BI4.0 SP02
  • Microsoft's AdventureWorks database
  • A UNX universe (required for use with CRE).
I'm Assuming
  • That you have a UNX universe created.
  • That you have Crystal Reports for Enterprise (CRE) or Crystal Reports 2008 or Crystal Reports 2011 installed. The screenshots are in CRE, but the steps are very similar in old-school Crystal.

First, the base report. This is close to how my report appears if I simply drag the objects onto the page.
image
It does the job, but is fairly unremarkable. So…

  1. Right click in the body section of the report and select Format Section…
    image
  2. Next to the Color option, click the formula icon. If you're not familiar with Crystal, these little icons allow you to set the value of a property at run-time with code.
    image

    The formula workshop will open up. This is largely the same in CRE as it is in CR 2008/2011.
  3. Enter the following code:
    image
  4. if Remainder(RecordNumber, 2) = 1
    then RGB(235,245,255)
    else RGB(255,255,255)

    Remainder is just like Mod, except it's called Remainder. RecordNumber is a sequential number that increments with each new row. This is a quick way to check if the row is an odd or even number. The logic then colours the background differently for odd and even rows. If you're new to Crystal syntax, remember that the formula you write must result in something that makes sense to the property that you're setting. In this case, that's a colour (or color).
  5. Click Save and Close down in the bottom right.
  6. Click Close to the format window. You should now see something like this:
    image
  7. Lastly, I'll add a line and set the header band and major fonts to blue.
    image
There's nothing ground-breaking here, but for a few minute's work, it makes the difference between an ordinary and a handsome report.

Have fun!