VS2010 报表设计器:在 RDLC 中将整数格式化为 (x)d、(y)h、(z)m [英] VS2010 Report Designer : Formatting an int as (x)d, (y)h, (z)m in RDLC
问题描述
我从我的数据库中返回一个 int 值,以分钟为单位表示时间.
I'm returning a int from my database which denotes time in minutes.
即如果列的值为 10,则表示 10 分钟.如果是 199,则表示 3 小时 19 分钟.
i.e. if the column's value is 10, it means 10 minutes. If it's 199, it means its 3 hours and 19 minutes.
然后我将我的结果分组,并计算一个 SUM(Fields!TotalTime.Value).
I then group my results, and calculate a SUM(Fields!TotalTime.Value).
如何将这个整数格式化为以下格式:(x)d, (y)h, (z)m, where
How can I format this int in the following format : (x)d, (y)h, (z)m, where
d = days
h = hours
m = minutes
使用内置函数?或者,我可以以某种方式编写自己的函数,例如 WPF 转换器吗?因为我在报表中分组,所以我无法从数据库中返回一个已经格式化的值.
using the built-in functions? Or, can I somehow write my own function, like a WPF Converter? Because I'm grouping in the report, I cannot return an already formatted value from the database.
推荐答案
其实可以做一个自定义转换器.
It is actually possible to do a custom converter.
在报表设计器中,右键单击报表,然后单击报表属性.在代码选项卡下,您可以指定报告中具体使用的代码.在那里创建您的转换器功能.就我而言:
When in report designer, right-click the report, and click report properties. Under the Code tab, you can specify code specifically being used in the report. Create your converter function there. In my case :
Public Function GenerateTimeString(ByVal minutes As Integer) As String
Dim returnString As String = ""
If minutes = 0 Then
returnString = "0m"
Else
Dim ts As TimeSpan = TimeSpan.FromMinutes(minutes)
If ts.Days <> 0 Then
returnString = returnString & ts.Days & "d,"
End If
returnString = returnString & ts.Hours & "d," & ts.Minutes & "m"
End If
Return returnString
End Function
然后,在您的列中调用它:
Then, call it in your columns with :
=Code.GenerateTimeString(Sum(Fields!TotalTime.Value))
这篇关于VS2010 报表设计器:在 RDLC 中将整数格式化为 (x)d、(y)h、(z)m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!