Custom functions
There are about 100 common functions in SSRS 2005, and they can
handle most of what you will need to do. Occasionally, however, you will still need
to set an expression with more control than the common functions can manage. So,
to make SSRS even more flexible, you can write custom VB.NET or C# functions and
use them in expressions.
Open the report and navigate to the Layout tab. From the Report menu
select Report Properties and then jump to the Code tab.
We are going to write a custom function that returns a different color, depending
on the value that is passed to it. We will then use that function to set the background
color for the status field in the report detail table.
Copy the following code into the code window:
Public Function GetColor(ByVal status as String) as String
IF status = "1" Then
Return "White"
End IF
IF status = "2" Then
Return "Yellow"
End IF
IF status = "3" Then
Return "Tomato"
End IF
End Function
Click OK and close the window.
Now that we have a function that returns color names we need to wire up that function
to an expression.
Click on the Customer Status cell and open the Properties window.
Find the Background Color property and choose Expression from the
dropdown. Add the following line of code to create an expression:
=code.GetColor(Fields!ChStatus.Value)
When the report runs and this expression is resolved, SSRS will call your GetColor
function and pass the value of the customer status for that instance of the row
to the function. The function takes over and decides which color should be returned.
SSRS then uses that value as the value for the background property for that cell.
Please note that custom functions must be called using =code.<myfunction>.
Now navigate to the Preview tab and run the report..
|