Showing posts with label RDLC. Show all posts
Showing posts with label RDLC. Show all posts

Friday, 18 November 2011

Conditional Formatting Using RDLC

I recently came across a problem on a report we needed to run that returned either a date or some text. The return value of the data then needed to be colour coded in a traffic light style so that dates in the past were red, dates in the future were Green and dates within the next month were orange. Additionally if the value was set to N/A then the field was to be turned gray.

Looking at the problem it seemed straight forward and I headed straight to the expression field of the color parameter for the column. I entered a switch statement that cast the value to the appropriate type (Date or string) and then attempted to do the logic. This turned into a large dead end as the casting of one type then seemed to not allow any further casts! After a few searches I couldn't find the answer so posted on the MSDN forums. After about 6 months of no answer I had pulled the last of my hair out and had put in a compromised solution.

The problem still nagged at me until I came across using code and shared assemblies in RDLC. To solve the problem I simply put a function into the code block and set the color parameter of the field to the expression. At last, a fix (although really its a workaround as it should have worked in the expression). Here is the function I put in:

 Public Function ColorFormat(ByVal cellValue As Object) As String
  
     Dim cellDate As Date
     Dim cellString As String
  
     cellString = CStr(cellValue)
  
     If Date.TryParse(cellString, cellDate) Then
       If (cellDate < Date.Now.AddMonths(1)) Then
         If (cellDate < Date.Now) Then
           Return "Red"
         Else
           Return "Orange"
         End If
       End If
     Else
       If String.Compare(cellString, "EXPIRED", True) = 0 Then
         Return "Red"
       End If
     End If
  
     Return "Black"
  
   End Function  

The functions in the code block need to be in VB and debugging whilst running is impossible so putting into a shared assembly is the preferred option.

Saturday, 15 October 2011

RDLC and Sub Reports

This week I have been looking at sub reports within RDLC and specifically dynamic reports where the dimensions of a table within the report are not known until runtime.  I managed to solve the dynamic table problem from an article posted here and adjusted the example to show the formatting required.  However, the report needed header and footer information so this looked to be quite troublesome editing the dynamic code. I also wanted the same header and footer information on a number of dynamic reports. Thats when I came across sub reports.

I created a basic RDLC with the required parameters to support the header and footer info and added a sub report. In the code I then populate a dataset with all the data required from my WCF middle tier and use the dynamic table functions to generate the report definition stream. This stream is then fed into the sub report and the report is shown in report viewer.  The only tricky part was sending the dataset into the sub report and I found the solution on the MSDN forums. I'll post up some code next week

Running is going ok at the moment and I'm five miles short of two hundred miles for the year. However, had a a pretty bad fall on Thursday and injured my hand quite badly.  Healing ok now but I had to pull half a branch out of my wrist which wasn't nice. Should crack the two hundred mile mark this weekend with some friends.