SSRS Report - Control Your Data Length in a table column per column line

Howard Cantrellretired
This code started out as a fix for a customer that had incoming data that was hunderds of numbers and words long that was to fit in one column. The problem was that the customer did not want to split words or numbers when wrapping in the column.

Ok the fun part was "how am I going to control the output to the column from the table? ". So the first thing I started working on was seeing how many characters would fit on a single line of the column that I need to use; and still look nice in the report.

Since the report had alot of data per detail row in the table I ended up with a column width length of 29 characters for this report. What was nice was that on this report the data was being split up by using a comma as a delimiter. Once the width was decided, the next step would be to wrap the incoming data so that each part of the data would wrap by design rather than arbitrary.

My favorite namespace in coding this type of data is using RegularExpressions; the use of RegularExpressions is only limited to your imagination. The code could be used with other applications. You would just have to experiment with it in the project that you are working on.

The code is pretty straight forward when you look at; just place the code in your report code tab properties. The delimiter can be just about any character you want. The sample uses the "comma" character as a delimiter. The InputString is the data that is coming in from you dataset; an example would be Trim(Fields!WordValues.Value) and the last field would be the width of your column. The sample below is using 29, meaning that the maxium number of characters on a single column row.

This is a sample of what the code would look like in the textbox or column cell of the table.


Open in new window

Sample out put would look something like this.....

Column A
                       chars, long, the, length, of 
                      ,970444, 77707,the, key, to, 
                      how, this, works

Open in new window

You will notice that the contents of "Column A" now wraps cleanly at a word or comma.

So, the only thing remaining is to show you the code for the FineDelimiter() function :

Public Function FineDelimiter(ByVal Delimiter As String, ByVal InputString As String, ByVal MaxLen As Integer) As String
                              Dim FirstRow As Boolean = True
                              Dim Counter As Integer = 0
                              Dim MaxCnt As Integer = 0
                              Dim strHolder As String = Nothing
                              If InputString = Nothing Then Return Nothing
                              For Each matchCurrent As String In  System.Text.RegularExpressions.Regex.Split(InputString, Delimiter)
                                  Counter += matchCurrent.Length + 1
                                  MaxCnt += matchCurrent.Length + 1
                                  If Counter > MaxLen Then
                                      FirstRow = False
                                      Counter = 0
                                  End If
                                  If FirstRow = True Then
                                      strHolder += matchCurrent + Delimiter
                                  ElseIf Counter = 0 Then
                                      If InputString.Length + 1 = MaxCnt Then
                                          strHolder += vbNewline & matchCurrent
                                          strHolder += vbNewline & matchCurrent + Delimiter
                                      End If
                                      Counter = matchCurrent.Length + 1
                                      If InputString.Length + 1 = MaxCnt Then
                                          strHolder += matchCurrent
                                          strHolder += matchCurrent + Delimiter
                                      End If
                                  End If
                              Next matchCurrent
                             If MaxCnt >= 2 And MaxCnt <= MaxLen Then
                                  Return strHolder.Substring(0, strHolder.Length - 1)
                                  Return strHolder
                              End If
                              Return Nothing
                          End Function

Open in new window

The only other consideration is taking care when deciding your character count (width) depending on the type of font you are using, and once determined, dont change the font :)

Howard Cantrellretired

Comments (3)

Nice idea friend, you saved me aq lot of time :D


For the last few weeks I have been playing with the print document control in VB.Net. Using X or Y patterns to print reports on the fly.
This article (above); that I made some years ago works very easy in the print document output very little code changing.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.