Sunday, January 31, 2010

SQL 2005 Reporting services: Create alternating colours in a table or matrix (Green bar effect)

Alternating colours in a table is fairly easy and can be done by setting the background colour on the row as follows:

Iif((RowNumber(Nothing) Mod 2 = 0), "White", "WhiteSmoke")

Doing the same in a matrix is a bit more complicated. There is probably several methods out there but I find the following one to be the easiest:

First create a function in the report code as follows

Private bOddRow As Boolean
'*************************************************************************
' -- Display green-bar type color banding in detail rows
' -- Call from BackGroundColor property of all detail row textboxes
' -- Set Toggle True for first item, False for others.
'*************************************************************************
Function AlternateColor(ByVal OddColor As String, _
ByVal EvenColor As String, ByVal Toggle As Boolean) As String
If Toggle Then bOddRow = Not bOddRow
If bOddRow Then
Return OddColor
Else
Return EvenColor
End If
End Function


Next set the row colours on the matrix by setting the background colour as follows:

Code.AlternateColor( "White","WhiteSmoke", True) ----> Set this on the matrix row group

Code.AlternateColor("White","WhiteSmoke", False) -----> Set this on the cell

A combination of effects can be achieved using this method such as a checker board effect by changing the True/False values and the colours.

No comments: