如何使用LINQ to SQL在Gridview Asp.Net中显示持续时间:小时:分钟:秒?

时间:2022-10-07 21:01:09

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

我希望通过使用LINQ to SQL从ASP.NET中的TimeCheckIn减去TimeCheckOut,在数据Gridview中显示持续时间仅为小时,分钟和秒

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:

这是我对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...

在Helper函数的第3行中,Dim dif作为TimeSpan = DateTime.Now.Subtract(Duration),它仅给出TimeCheckIn中的Hour,Minute和Second持续时间,直到DateTime.Now。但是,我希望从TimeCheckIn到TimeCheckOut只有小时,分钟和秒的持续时间。我知道FieldDisplayDuration函数是完全错误的逻辑,但我只是想让你得到我的观点,而且它也可能是那些想要从雇用日期计算员工持续时间的人的代码示例。最后,让我们通过在Gridview问题中从TimeCheckIn中减去TimeCheckOut来回到TimeSpan,我该怎么做?请给我一些线索..非常感谢你...

2 个解决方案

#1


It is possible to just subtract the date and time to get the Timespan so you should be able to use

可以简单地减去获取Timespan的日期和时间,以便您能够使用

 Text='<%# FieldDisplayDuration(Eval("TimeCheckIn"), Eval("TimeCheckOut")) %>'

and something like this function:

和这样的功能:

Protected Function FieldDisplayDuration(ByVal CheckIn As DateTime, ByVal CheckOut as DateTime) As String    
    Dim rtn As String = "DefaultValue"    
    Dim dif As TimeSpan = CheckOut - CheckIn        
    rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "    
    Return rtnEnd 
Function

Though I would probably use a string format on the rturn line and do this...

虽然我可能会在rturn行上使用字符串格式并执行此操作...

rtn = string.Format("{0} hours, {1} minutes, {2} seconds.", dif.Hours, dif.Minutes, difSeconds)

As I think it is easier to read.

我觉得它更容易阅读。

#2


You can put the logic to compute the duration right in your Select clause, like so:

您可以使用逻辑来计算Select子句中的持续时间,如下所示:

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.timeCheckOut.Subtract(u.timeCheckIn)

Then, you only need to format the resulting Timespan.

然后,您只需要格式化生成的Timespan。

Alternatively, you could do it all in the select, or even call a function, so have something like

或者,你可以在选择中完成所有操作,甚至可以调用一个函数,所以有类似的东西

Duration = FormatDuration(u.timeCheckOut.Subtract(u.timeCheckIn)

or

Duration = FormatDuration(u.timeCheckIn, u.timeCheckOut)\

If you did this, and assuming FormatDuration returned a string, you would be able to do away with the column template entirely

如果您这样做,并假设FormatDuration返回一个字符串,您将能够完全取消列模板

#1


It is possible to just subtract the date and time to get the Timespan so you should be able to use

可以简单地减去获取Timespan的日期和时间,以便您能够使用

 Text='<%# FieldDisplayDuration(Eval("TimeCheckIn"), Eval("TimeCheckOut")) %>'

and something like this function:

和这样的功能:

Protected Function FieldDisplayDuration(ByVal CheckIn As DateTime, ByVal CheckOut as DateTime) As String    
    Dim rtn As String = "DefaultValue"    
    Dim dif As TimeSpan = CheckOut - CheckIn        
    rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "    
    Return rtnEnd 
Function

Though I would probably use a string format on the rturn line and do this...

虽然我可能会在rturn行上使用字符串格式并执行此操作...

rtn = string.Format("{0} hours, {1} minutes, {2} seconds.", dif.Hours, dif.Minutes, difSeconds)

As I think it is easier to read.

我觉得它更容易阅读。

#2


You can put the logic to compute the duration right in your Select clause, like so:

您可以使用逻辑来计算Select子句中的持续时间,如下所示:

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.timeCheckOut.Subtract(u.timeCheckIn)

Then, you only need to format the resulting Timespan.

然后,您只需要格式化生成的Timespan。

Alternatively, you could do it all in the select, or even call a function, so have something like

或者,你可以在选择中完成所有操作,甚至可以调用一个函数,所以有类似的东西

Duration = FormatDuration(u.timeCheckOut.Subtract(u.timeCheckIn)

or

Duration = FormatDuration(u.timeCheckIn, u.timeCheckOut)\

If you did this, and assuming FormatDuration returned a string, you would be able to do away with the column template entirely

如果您这样做,并假设FormatDuration返回一个字符串,您将能够完全取消列模板