4

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!

7 Answers 7

6
=IIF(RunningValue(Fields!Address.Value, CountDistinct, Nothing) MOD 2 = 1, "White", "Lavender")

For me this does the trick.

1

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.

1
  • Cam - First off, BRILLIANT code! I had to stare at it for a while before I saw what you were doing. If I apply this to the BackgroundColor on a single cell, it works very well. However, if I apply it to more than one cell (which is what happens when you apply it to a full row), it doesn't work properly. I am pretty sure that this is because it increments the counter for EACH CELL. You can see an example of the results here: imageshack.us/photo/my-images/839/alternatingcolors2.jpg Can you think of a way to make this work for an entire row? THANK YOU very much for your help!
    – Loki70
    Jul 21, 2011 at 1:44
1

Got it - I should have tried harder before replying. I had to keep track of the current row number and only switch the value on new rows. Revised code:

Private _dateCount As Integer = 0
Private CurRowNumber as Integer = 0
Private ColorValue as String = ""

Function GetDateRowColor(ByVal previousDate As DateTime, ByVal currentDate As DateTime, MyRowNumber as Integer) As String

    'Check if this is a new row number...
    If MyRowNumber <> CurRowNumber then
       CurRowNumber = CurRowNumber  + 1 'New row, so increment counter

        If previousDate = currentDate Then
            ' Do nothing
        Else
                _dateCount = _dateCount + 1
        End If

        If _dateCount Mod 2 = 0 Then
                ColorValue = "White"
        Else
                ColorValue = "Lavender"
        End If
   End If

        Return ColorValue 'Always return a value (for columns other than the first one)

End Function

Called like this:

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

Thank you again for your excellent response & answer!

2
  • Have you experienced issues, on the page 2, for some reasons, the above code runs perfect on page 1, but on page 2, it doesn't select rows correctly.
    – Nanu
    Nov 15, 2012 at 17:24
  • Nope. My report is several pages long, and the color coding works correctly over all of them.
    – Loki70
    Nov 30, 2012 at 15:37
1

I had a similar problem:

  • Tablix/Table in SSRS 16
  • No grouping possible (would interfere with other functionality of the tablix)
  • Need to alternate colour blocks of rows with same value in date field
  • same date value could appear again (this is important, because Nanus Answer i.e. the use of CountDistinct depends on the same value (date) not appearing again in a later block).

For me the code in Loki70 revised answer didn't work. The first line of few random blocks of rows would have alternating colours in the cells. However once I rewrote the code it worked:

Private _dateCount As Integer = 0
Private RowNumberRunner as Integer = 0
Private ColorValue as String = ""

Function GetDateRowColor(ByVal previousDate As DateTime, ByVal currentDate As DateTime, MyRowNumber as Integer) As String

   If MyRowNumber <> RowNumberRunner Then 
      RowNumberRunner = MyRowNumber
      If previousDate <> currentDate Then 
         _dateCount = _dateCount + 1
      End If
   End If

   If _dateCount Mod 2 = 1 Then 
      ColorValue = "White"
   Else ColorValue = "Lavender"
   End If

   Return ColorValue 
End Function

No clue, why that works and the previous code didn't. It's the same functionality, just written differently. It's called the same way:

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

I had the same issue that Loki70 had and really liked Nanu's solution.

However, once I saw the result I wanted to do more. I wanted to have the primary information appear once but "hide" the rows after the first row of a group. Using Nanu's and Loki70's code together I was able to set the font color of the rows after the first row to the same color as the fill. Thereby hiding the text for that cell.

=IIF(Previous(Fields!Req_Del_Dt.Value) <> (Fields!Req_Del_Dt.Value), "Black", 
    IIF(RunningValue(Fields!Req_Del_Dt.Value, CountDistinct, Nothing) MOD 2 = 1, 
    "White", "Lavender"))

I use this to hide the first couple cells of a row which display the same information, and then display the other cells that are different for that subgroup.

-1
Public Function Setcolor(ByVal Runs AS Integer,ByVal Wickets AS Integer) AS String
    setcolor="Transperent"
    IF(Runs >=500 AND Wickets >=10) THEN return "Green"
    END IF
END Function
-1

here's a simple solution. first, i'm going to assume that you're working with MSSQL, since the question is about SSRS. You can select values directly from the query, so in the report itself, you just set background color according to a single value, and not a range...

let's say, you want to present in report Req_Del_Dt.Value with a different color, according to it's range.... so, you can query something like this>

select *,
    case when Req_Del_Dt < 30 then 1
    when Req_Del_Dt between 30 and 60 then 2
    when Req_Del_Dt between 61 and 90 then 3
    when Req_Del_Dt between 91 and 150 then 4
    else 5 end as color_range
from source_table

having that, in SSRS, you just go to the BackgroundColor property, in the Fill section of the textbox where you're displaying req_del_det, select color expression, and write something like this>

    =SWITCH(Fields!color_range.Value = 1, "#ffffff", 
    Fields!color_range.Value = 2, "#ffebeb",
    Fields!color_range.Value = 3, "#ffd8d8",
    Fields!color_range.Value = 4, "#ffc4c4",
    Fields!color_range.Value = 5, "#ffb1b1")
1
  • If you read my question more thoroughly, I wasn't just using the value in the cell to determine the row color - I was using the value in the previous row, which was covered in the accepted answer. Thanks anyhow.
    – Loki70
    Jan 26, 2016 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.