SSRS: Get backgroundcolor from Cell

我的未来我决定 提交于 2019-12-02 05:42:16

What you need to do is to remember the colour setting for the column so you can reuse it in headers, footers, etc. We can achieve this with an array which returns a new colour first time called and the same colour for that index if that colour was previously assigned.

Here is the custom code:

Dim Colors(0 to 9) As String

Function GetColor(Color As Integer) As String
  Dim intHighNumber AS Decimal = 255 
  Dim intLowNumber AS Decimal = 100 
  Dim ThisColor AS String 

  if (Colors(Color) <> "") Then
    ThisColor = Colors(Color) 
  else 
    Dim Red AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    Dim Green AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    Dim Blue AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    ThisColor = "#" & Hex(Red) & Hex(Green) & Hex(Blue) 
    Colors(Color) = ThisColor
  End If 

  Return ThisColor 
End Function

Then in your cells, you simply call the GetColor function passing the colour index number that you want the cell to have. For instance, the first column's header, detail, and footer cells' BackgroundColor property expression would all be:

=Code.GetColor(0)

Then you just use a different colour index for each column.

Make sure you dimension the array with enough room to store all your colours (or you could also ReDim your array each time you add a new colour if you want to use a dynamic array instead).

Private string _LastColorUser="" 

Public Function LastColorUsed()
  Return _LastColorUsed
End Function

Public Function GetColor()

    Dim intHighNumber AS Decimal = 255
    Dim intLowNumber AS Decimal = 100

    Dim NewColor AS String
    Dim Red AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Green AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Blue AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)


    NewColor = "#" & Hex(Red) & Hex(Green) & Hex(Blue)

    _LastColorUser=NewColor;  

    Return NewColor
End Function

@Chris Thank you this is what i needed. Because my Columns were dynamic I had to write another function to get a Number:

Public Function GetColumnNumber(ByVal parameter as Parameter,ByVal SSID As String) as String
      For i as integer = 0 to parameter.Count-1
         If CStr(parameter.Value(i)) = SSID THEN
          Return i
         END IF
      Next
End Function

In the Cell i wrote this:

=code.GetColor(code.GetColumnNumber(Parameters!SSID,Fields!SSID.Value))

Thanks again :)

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