我想通过使用 LINQ to SQL 从 ASP.NET 中的 TimeCheckIn 中减去 TimeCheckOut 来在数据 Gridview 中仅显示持续时间 Hour、Minutes 和 Second
这是后面的代码:
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 函数是完全错误的逻辑,但我只想让你明白我的意思,它也可能是那些想要计算员工从雇佣日期开始的持续时间的代码示例。最后,让我们通过从 gridview 问题中的 TimeCheckIn 中减去 TimeCheckOut 来回到 TimeSpan,我该怎么做?请给我一些线索..非常感谢你...