Search code examples
ssrs-2008reporting-services

Changing Row Colors in SSRS Report via data values


I know you can set the BackgroundColor to alternate between two colors with a fairly simple expression. I have a column that contains date values organized in order. Basically, I want the BackgroundColor to alternate each time the date value changes as you go down the rows. I got partway there with this code:

=iif(Previous(Fields!Req_Del_Dt.Value) = (Fields!Req_Del_Dt.Value), "White", "Lavender")

This will change the color each time the value of a row is not the same as the previous row. This is what the results of this look like:

http://imageshack.us/photo/my-images/24/alternatingcolors.jpg/

How can I make it so that the color changes to one color for an entire date (which might be 3 rows) and then "toggle" to a different color when the next date change occurs? I think I am on the right track, but I just can't figure this one out.

I would greatly appreciate any suggestions or comments. Thank you!


Solution

  • You can write custom code. For example:

    Private _dateCount As Integer = 0
    Function GetDateRowColor(ByVal previousDate As DateTime, ByVal currentDate As DateTime) As String
        If previousDate = currentDate Then
            ' Do nothing
        Else
            _dateCount++
        End If
    
        If _dateCount Mod 2 = 0 Then
            Return "White"
        Else
            Return "Lavender"
        End If
    End Function
    

    Then, use expression in your Background color, for example:

    =Code.GetDateRowColor(Previous(Fields!Req_Del_Dt.Value), Fields!Req_Del_Dt.Value)
    

    HTH.