我想使用LINQ在ASP.NET中顯示來自TimeCheckIn減去TimeCheckOut數據GridView中唯一的持續時間時,分,二來是SQL如何僅顯示持續時間小時:分鐘:通過使用LINQ to SQL的Gridview Asp.Net中的第二個?
這裏是後面的代碼:
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
在輔助函數的第3行中,Dim Dif爲TimeSpan = DateTime.Now.Subtract(Duration),它僅給出TimeCheckIn的Hour,Minute和Second的持續時間,直到DateTime.Now。但是,我想要從TimeCheckIn到TimeCheckOut的持續時間僅限於小時,分鐘和秒。我知道FieldDisplayDuration函數是完全錯誤的邏輯,但我只想讓你明白我的觀點,也可以是那些想從僱用日期計算員工持續時間的代碼示例。最後,讓我們回到TimeSpan,通過在GridView問題中從TimeCheckIn中減去TimeCheckOut,我該怎麼做?請給我一些線索..謝謝你這麼多...
非常感謝。它的作品非常甜... – Vicheanak 2009-05-18 04:01:01