Alternate line Colors in Reports Using Crystal Reports


Sometimes when we have a big reports displaying heavy textual information, one technique to visualize data in a consistent way is to shade line in alternate colors. In this article, I will show how Crystal Reports enables this technique very easily.

Getting started with the solution

The first step is to create a testing solution and add a Crystal Reports artifact. Let's suppose we want to show information about our customers using the following SQL query on the several tables in the AdventureWorks database (see Listing 1).

select c.CustomerID, c.CustomerType, ad.AddressLine1, ad.AddressLine2, ad.City, sp.[Name] 
as StateName, sp.CountryRegionCode as CountryCode, ad.PostalCode
from Sales.Customer c inner join Sales.CustomerAddress ca on c.CustomerID=ca.CustomerID
                      inner join Person.Address ad on ca.AddressID=ad.AddressID
                      inner join Person.StateProvince sp on ad.StateProvinceID=sp.StateProvinceID

Listing 1

To implement the logic, we need to use the Mod function which performs modulus arithmetic by returning the remainder of the division as well as the RecordNumber function which simply returns the number of the actual record in the report result set.

The create a title for the report page header and drag and drop the CustomerID, CustomerType, StateName and CountryCode fields onto the detail section of the report.

Next step is to right-click on any blank region of the report and select Report | Section Expert option from the context menu. In the Section Expert window, go to the Details section and click on the Color tab (see Figure 1).

Figure 1

Then click on the X+2 icon to apply a formula to the color selection through the Format Formula Editor. And enter the logic as shown in the Figure 2.

Figure 2

You can see that we're using the crNoColor constant instead crWhite, because crWhite will show the alternating color with a degree of transparency.

Now create a form to display the report and a report document referencing the previously created report using the following code to bind the report data source with the data return the database using strongly dataset (see Listing 2).

private void dfrmAlternateColorCrystalReport_Load(object sender, EventArgs e)
    DS_Client dsClient = new DS_Client();
    CustomerTableAdapter daCustomer = new CustomerTableAdapter();

Listing 2

After you run the application, you will get the following report (see Figure 3).

Figure 3


In this article, I've shown how to visualize data using alternate colors using Crystal Reports.

Similar Articles