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

发布于 2024-07-22 05:47:48 字数 2168 浏览 5 评论 0原文

我想通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-07-29 05:47:48

可以只减去日期和时间来获取时间跨度,因此您应该能够使用

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

类似这样的函数:

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

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

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

正如我认为的那样更容易阅读。

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

 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...

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

As I think it is easier to read.

还在原地等你 2024-07-29 05:47:48

您可以将计算持续时间的逻辑放在 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)

然后,您只需格式化结果时间跨度。

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

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

或的

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

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文