SSRS中的条件格式与代码划分

时间:2017-02-14 13:06:14

标签: vb.net tsql reporting-services

回到SSRS问题。这是场景..

我的报告包含一个应该具有条件格式的单元格,其中包含以下逻辑:

If AV has value:
= 0    ;   Then value has to be in "Silver"         (White)
<= 0.6 ;   Then value has to be in "CornflowerBlue" (Blue)
<= 0.8 ;   Then value has to be in "Tomato"         (Red)
> 0.7  ;   Then value has to be in "Gold"           (yellow)
> 0.6  ;   Then value has to be in "LightGreen"     (Green)

然后我创建了我的代码鸿沟......

Public function ColorValue(ByVal _AV as Double) As Double

IF _AV = 0 Then 
    Return "silver"

Else
    IF _AV >= (0.8) Then
        Return 'Tomato'
    Else
        IF _AV >= (0.7) Then
            Return 'Gold'
        Else
            IF _AV >= (0.6) Then
                Return 'LightGreen'
            Else        
                Return 'CornflowerBlue'
            END IF
        END IF
    END IF
END IF              

End Function

并继续收到此错误: enter image description here 我之前从未在VB中创建过函数,因此我将代码划分为现有函数..

 Public Function FillLowValue(ByVal _target As Double, ByVal _strValue
 As String) As String   Dim _value As Double        If _target = Nothing
 Then

        Return "Silver"
    End If

    If _strValue = "N/A" Then
        Return "Silver"
    Else
        Double.TryParse(_strValue, _value)
    End If

    If _value < (_target - ( _target  * 0.2 )) Then
        Return "CornflowerBlue"
    Else
        If _value >= (_target + ( _target * 0.2 )) Then
            Return "Tomato"
        Else
            If _value <= _target Then
                Return "LightGreen" 
            Else
                Return "Gold"
            End If
        End If
    End If

        Return "Silver"
    End Function

我也搜索了以下错误,并且在我的搜索中没有运气到达第10页。希望有人能至少给我一个想法,看看哪里。

PS:另外,你能检查我的IF逻辑吗?

还尝试了这种方法,报告接受但没有对细胞本身返回任何价值

    Public Function ColorValue(ByVal _AV As Double) As String

If value = 0 Then
                Return "Silver"
            ElseIf value >= 0.8 Then
                Return "Tomato"
            ElseIf value >= 0.7 AND value < 0.8 Then
                Return "LightGreen" 
            ElseIf value >= 0.6 AND value < 0.7 Then
                Return "Gold"
            ElseIf value > 0.6 Then
                Return "CornflowerBlue"
End If

End Function

提前致谢

2 个答案:

答案 0 :(得分:1)

最好使用Select Case语句,如下所示。但是,您的限制也有问题...&lt; = 0.8与&gt;冲突问题中写的0.6和> 0.7。我假设您的代码意味着&gt; = 0.8。

Double的返回类型在这里也无效。因为我不知道你真正想要的是什么,所以我把它换成了颜色。

Public Function ColorValue(ByVal _AV As Double) As Color
    Select Case _AV
        Case 0 : Return Color.White
        Case Is >= 0.8 : Return Color.Tomato
        Case Is <= 0.6 : Return Color.CornflowerBlue
        Case Is > 0.7 : Return Color.Gold
        Case Else : Return Color.LightGreen
    End Select
End Function

如果你真的需要它作为数字,请改用它。

Public Function ColorValue(ByVal _AV As Double) As Integer
    Select Case _AV
        Case 0 : Return Color.White.ToArgb
        Case Is >= 0.8 : Return Color.Tomato.ToArgb
        Case Is <= 0.6 : Return Color.CornflowerBlue.ToArgb
        Case Is > 0.7 : Return Color.Gold.ToArgb
        Case Else : Return Color.LightGreen.ToArgb
    End Select
End Function

如果您需要颜色名称....那么

Public Function ColorValue(ByVal _AV As Double) As String
    Select Case _AV
        Case 0 : Return "White"
        Case Is >= 0.8 : Return "Tomato"
        Case Is <= 0.6 : Return "CornflowerBlue"
        Case Is > 0.7 : Return "Gold"
        Case Else : Return "LightGreen"
    End Select
End Function

如果你真的需要加倍...那么......

Public Function ColorValue(ByVal _AV As Double) As Double        
    Select Case _AV
        Case 0 : Return CDbl(Color.White.ToArgb)
        Case Is >= 0.8 : Return CDbl(Color.Tomato.ToArgb)
        Case Is <= 0.6 : Return CDbl(Color.CornflowerBlue.ToArgb)
        Case Is > 0.7 : Return CDbl(Color.Gold.ToArgb)
        Case Else : Return CDbl(Color.LightGreen.ToArgb)
    End Select
End Function

虽然我完全不确定那些都能正常转换。

答案 1 :(得分:0)

您似乎从ColorValue函数返回了无效值。

在第6行中,您使用单引号返回Return 'Tomato'。你必须为字符串使用双引号。

Return "Tomato"

Return "Gold"

Return "LightGreen"

Return "CornflowerBlue"