Changing Row Colors in SSRS Report via data values

隐身守侯 提交于 2019-12-06 13:46:23

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.

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

For me this does the trick.

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!

mssgh23

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))

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.

vijayakumar
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

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!