I was recently working on creating a SSRS report in SQL Server 2008 which required alternating background colors. Initially it seemed a easy requirement but as the report got built out it had a row group and a column group which made the getting background colors tricky. Initially I went with the RowNumber and Running value functions, it did not get the required effect. In an earlier blog post i had mentioned the use of DENSE_RANK() function in a stored procedure to get the background color effect. I recently came across another approach which is using Group variables(I would like to acknowledge the MSDN forums and blogger's who have mentioned this approach), which works very well. First you would need a custom code function which would go in the Report Properties code section:
Public EvenRow as Boolean
Public Function AltRow() as Boolean
EvenRow = Not EvenRow
Return EvenRow
End Function
Once this is done, In the group properties (this would be the data point/column in the dataset for which we need the alternate row color), under variables create a variable called AltRowColor, in the value section call the function =Code.AltRow()
Select the whole row for which you need the alternate row color, in the background color property enter the following expression: =iif(Variables!AltRowColor.Value,"Gainsboro","White"). Once this is completed run the report you should see the alternate row colors. This was done in SSRS 2008 with the tablix control which has Row and Column Groups.
Thank you for posting this valuable information. I implemented and it worked great!
ReplyDeletePerfect!
ReplyDeleteYou are awesome! Thank you!
ReplyDeleteThank you all for your comments.
ReplyDelete2 years later and just as useful, had spent hours on this. Clean and simple, perfect!
ReplyDeleteThank you @Anonymous. This is one area where i feel SSRS as aproduct could improve upon grouping and background colors. It is one of the most requested requirement in a report.
ReplyDeletethanks You so Much.it solves my problem
ReplyDeleteThanks you soo much..it works fine
ReplyDeleteThank you Vadei, glad it worked out for you.
DeleteThanks for reading my blog.
i tried everything else nothing works and this on works so good .Thanks you so much. nice and simple.
ReplyDeleteThanks Drashan, Glad it worked out for you.
DeleteThank you
perfect, very useful post. thanks a million!
ReplyDeleteDoes not work at the Report Level
ReplyDelete* Report Properties / Variables:
* Tax = 0.08 [read only]
* Report Properties / [Custom] Code:
Public Shared Function functionTax(ByVal param as Double) as Double
Return param * Variables!Tax.Value
End Function