Rosebud
Rosebud

Reputation: 589

SSRS 2012 Alternating Group Colour within rows

hi I have a simple SSRS 2012 report with a table. All the data is displayed via store procedure. There is no grouping i.e

Dept Name Age
ClassA Bob 15
ClassA Sue 16
ClassA Jo 15

ClassB Sam 15
ClassB Jack 15
ClassB Mary 16
ClassC Pete 15
ClassC Sue 16

ClassD Jo 15
ClassD Mary 14
ClassD Jack 14

I would like rows colour block for each 'group' to change i.e White and then Blue

many thanks

Upvotes: 0

Views: 181

Answers (4)

MyLittleDax
MyLittleDax

Reputation: 111

I use something like this for various alternate colour scenarios.

In the custom code for the report:

Private CountArr(,) AS String
Private eleID AS Integer
Public Function AlternateColours(OddCol AS String, EvenCol AS String, RowID AS String, UserID AS Integer)

    If UserID > eleID
        eleID = UserID
        ReDim Preserve CountArr(1,eleID -1)
    End If

    If CountArr(0,UserID-1) <> RowID
        CountArr(0,UserID-1) = RowID
        CountArr(1, UserID-1) += 1
    End If

    If CountArr(1, UserID-1) MOD 2 <> 0
        Return OddCol
    Else
        Return EvenCol
    End If

End Function

Public Function ResetAlternateColours(Value AS String)

    ReDim CountArr(1, 0)
    eleID = 0
    Return Value

End Function

Instructions

OddCol is the first colour, EvenCol is the alternate colour and RowID is a string that determines when a Row change has occurred -- This can be any value that correctly identifies a new row such as a concatenation of field values or even just an arbitrary string for rows that aren't grouped. (For your requirements your parameter would be Fields!Dept.Value)

UserID is an Integer value chosen by the user to identify where the row is located in the array. Recommended use is to provide numbers in the order that the report is rendered starting from 1.

FYI: Reports are rendered in reading order i.e. Left-to-right, top-to-bottom.

The reset function is also included in the event you have areas where you would like the alternating colours to start from the beginning. For my personal use, I had it take a string as a value and return that value after the reset.

=Code.ResetAlternateColours(Fields!Total.Value)

It is not necessary and you can remove the return value from the function and then just add it to the end of the cell.

=Fields!Total.Value + Code.ResetAlternateColours()

Just remember rendering orders and you should be fine.

This function can be used for columns, rows, cells and anything else you can think of as long as you remember the render order and increment the UserID accordingly.

For your use:

=Code.AlternateColours("White", "Blue", Fields!Dept.Value, 1)

Upvotes: 0

Simon
Simon

Reputation: 1333

I have been looking for a solution to the same thing for a while and just stumbled across this solution.

Adding the following expression to the background colour property of the row that is being grouped will alternate the colours of the groups between Gainsboro and White.

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

Where Fields!Category.Value is the value from your stored procedure you are grouping on.

I hope this is what you were looking for.

Thanks

Upvotes: 0

Chris Latta
Chris Latta

Reputation: 20560

In Report-Report Properties... on the Code tab insert the following code:

Dim LastDept AS String = ""
Dim Color AS String

Function GetColor(Dept As String) AS String
  if (Dept <> LastDept) Then
    if (Color = "White") Then
      Color = "PowderBlue"
    Else
      Color = "White"
    End If
  End If
End Function

Then set the BackgroundColor expression of your row to

=Code.GetColor(Fields!Dept.Value)

The will toggle the background colour of your rows between White and PowderBlue whenever the department changes.

Upvotes: 0

M.Ali
M.Ali

Reputation: 69494

You can use the following expression (Backgroud , Text ) where ever you want your report to change colours.

=Switch(
        Fields!Dept.Value = "ClassA" , "Red"
      , Fields!Dept.Value = "ClassB" , "Orange"
      , Fields!Dept.Value = "ClassC" , "Green"
      , Fields!Dept.Value = "ClassD" , "Yellow"
      )

Upvotes: 1

Related Questions