Custom .ToString() Formats in .rdlc Reports

不打扰是莪最后的温柔 提交于 2019-12-11 01:49:32

问题


I have a custom business object which overloads the .ToString() function. It also implements IFormattable.ToString, so I can define my own custom formats.

This approach seems to work everywhere in my app, except .rdlc reports. For example, I have a text field on a report with the following expression:

=Fields!MyField.Value.ToString("lr")

"lr" is a custom format I have created. When running the report I always get #Error as the output. I've placed breakpoints in my .ToString function and stepped through the code as the report is running, and I know the function is returning the correct value, but the report just doesn't seem to be capable of receiving and displaying it.


回答1:


it is possible to do in a different way in the rdlc report you can use VB code. Just click on the report with right mouse button. When the context menu from where you enter parameters go to Proprties. When clicking it you should see a tab control witch few tab pages. Go to tab page "Code" an there right you VB function it must be something like this

 Public Function ParseVal(ByVal val As Object ) As String
     Dim MyClass As obj = CType(val, MyClass )
     return obj.ToString( "lr" ) 
    End Function

Then call the function in your textbox like this

= Code.ParseVal(Fields!MyField.Value )

P.S. I am not very sure if the VB code is working correctly just test it and if it is needed rewrite. If any other error occurs please post it to see what is the problem



来源:https://stackoverflow.com/questions/2362185/custom-tostring-formats-in-rdlc-reports

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