SSRS How to change background color after a field

2019-09-16 06:10发布

问题:

I have a tablix table with hundreds of rows of data. I would like to have the background color to be yellow right after a particular condition. How can I do that?

Example - I want all the data to be yellow iif(Fields!pet.Value="cat"), so hamster and on...

回答1:

You need to change the expression for the cell's BackgroundColor and remember what it was. We'll need custom code for this. Go to the Report menu, choose Report Properties... and click on Code and enter the following code:

Dim ThisPetColor As String

Function PetColor(PetType As String) As String
    If ThisPetColor = "" Then
        ThisPetColor = "White"
    End If

    If PetType = "cat" Then
        ThisPetColor = "Yellow"
    Else If PetType = "hamster" Then
        ThisPetColor = "Green"
    Else If PetType = "unicorn" Then
        ThisPetColor = "Purple"
    End If

    Return ThisPetColor 
End Function

Then in your cell make the BackgroundColor expression be:

=Code.PetColor(Fields!pet.Value)

or for even more flexibility in copying the formula, use the built-in Me reference for the textbox:

=Code.PetColor(Me.Value)

So what we are doing is creating a global variable for the pet color ThisPetColor and initialising that to "White". When the type of pet changes, if it is one of the ones where we change color then we change the value of the global variable to be the new pet color and use that thereafter (until the next pet type).