如何使用 LINQ to SQL 在 Gridview Asp.Net 中仅显示持续时间小时:分钟:秒?
我想通过使用 LINQ to SQL 从 ASP.NET 中的 TimeCheckIn 中减去 TimeCheckOut,在数据 Gridview 中仅显示持续时间小时、分钟和秒
这里是代码隐藏:
Dim db = new MyDataContext
Dim user = from u in db.Employees select IDNumber = u.IDNumber, _
FirstName = u.firstName, LastName = u.lastName, TimeCheckIn = u.timeCheckIn, _
TimeCheckOut = u.timeCheckOut, Duration = u.timeCheckIn
Gridview1.DataSource = user
Gridview1.DataBind()
页面上的代码:
<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="IDNumber" HeaderText="ID Number" ReadOnly="True" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True"/>
<asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True"/>
<asp:BoundField DataField="TimeCheckIn" HeaderText="Time Check In" ReadOnly="True"/>
<asp:BoundField DataField="TimeCheckOut" HeaderText="Time Check Out" ReadOnly="True" />
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# FieldDisplayDuration(Eval("Duration")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的 FieldDisplayDuration 帮助程序:
Protected Function FieldDisplayDuration(ByVal Duration As DateTime) As String
Dim rtn As String = "DefaultValue"
Dim dif As TimeSpan = DateTime.Now.Subtract(Duration)
rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "
Return rtn
End Function
在 Helper 中的第 3 行函数 Dim dif as TimeSpan = DateTime.Now.Subtract(Duration) 只给出从 TimeCheckIn 到 DateTime.Now 的小时、分钟和秒的持续时间。 但是,我想要从 TimeCheckIn 到 TimeCheckOut 的持续时间仅以小时、分钟和秒为单位。 我知道 FieldDisplayDuration 函数是完全错误的逻辑,但我只想让您明白我的观点,对于那些想要计算员工从雇用日期开始的持续时间的人来说,它也可能是代码示例。 最后,让我们回到 TimeSpan,在 gridview 问题中从 TimeCheckIn 中减去 TimeCheckOut,我该怎么做? 请给我一些线索。非常感谢你...
I want to display the duration only Hour, Minutes, and Second in data Gridview by Subtract TimeCheckOut from TimeCheckIn in ASP.NET using LINQ to SQL
Here is code behind:
Dim db = new MyDataContext
Dim user = from u in db.Employees select IDNumber = u.IDNumber, _
FirstName = u.firstName, LastName = u.lastName, TimeCheckIn = u.timeCheckIn, _
TimeCheckOut = u.timeCheckOut, Duration = u.timeCheckIn
Gridview1.DataSource = user
Gridview1.DataBind()
Code on page:
<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="IDNumber" HeaderText="ID Number" ReadOnly="True" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True"/>
<asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True"/>
<asp:BoundField DataField="TimeCheckIn" HeaderText="Time Check In" ReadOnly="True"/>
<asp:BoundField DataField="TimeCheckOut" HeaderText="Time Check Out" ReadOnly="True" />
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# FieldDisplayDuration(Eval("Duration")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my helper for FieldDisplayDuration:
Protected Function FieldDisplayDuration(ByVal Duration As DateTime) As String
Dim rtn As String = "DefaultValue"
Dim dif As TimeSpan = DateTime.Now.Subtract(Duration)
rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "
Return rtn
End Function
In Line 3 in Helper function, Dim dif as TimeSpan = DateTime.Now.Subtract(Duration) which give only the duration of Hour, Minute, and Second from TimeCheckIn until DateTime.Now. However, I want to have the duration from TimeCheckIn until TimeCheckOut only in Hour, Minute, and Second. I know that the FieldDisplayDuration function is totally wrong logic, but I just want you to get my point only, and also it could be the code sample for those who want to calculate the duration of the employee from the hire date. Finally, Let's get back to TimeSpan by Subtract TimeCheckOut from TimeCheckIn in gridview problem, How can I do that? Please give me some clue.. Thanks you so much...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以只减去日期和时间来获取时间跨度,因此您应该能够使用
类似这样的函数:
虽然我可能会在 rturn 行上使用字符串格式并执行此操作...
正如我认为的那样更容易阅读。
It is possible to just subtract the date and time to get the Timespan so you should be able to use
and something like this function:
Though I would probably use a string format on the rturn line and do this...
As I think it is easier to read.
您可以将计算持续时间的逻辑放在 Select 子句中,如下所示:
然后,您只需格式化结果时间跨度。
或者,您可以在选择中完成所有操作,甚至调用一个函数,所以有类似
或的
东西如果您这样做,并假设 FormatDuration 返回一个字符串,您将能够完全取消列模板
You can put the logic to compute the duration right in your Select clause, like so:
Then, you only need to format the resulting Timespan.
Alternatively, you could do it all in the select, or even call a function, so have something like
or
If you did this, and assuming FormatDuration returned a string, you would be able to do away with the column template entirely