开发者

Changing Row Colors in SSRS Report via data values

I know you can set the BackgroundCo开发者_运维问答lor 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!


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

For me this does the trick.


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.


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!


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜