【问题标题】:VB.NET how i can calculate late time in datagridviewVB.NET 如何计算 datagridview 中的延迟时间
【发布时间】:2016-07-26 11:24:24
【问题描述】:

我需要帮助计算值班的迟到时间 - 打卡但我的代码不起作用, 这实际上不打卡 - 打卡我只是举个例子。\

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    For Each row As DataGridViewRow In frmExcelGrid.Rows
        Dim clock1 As DateTime
        Dim clock2 As DateTime
        Dim total As TimeSpan
        clock1 = DateTime.Parse(row.Cells("clock in").Value)
        clock2 = DateTime.Parse(row.Cells("clock out").Value)
        total = clock1 - clock2
        row.Cells("total").Value = total
     Next


End Sub

image

【问题讨论】:

  • total 到底显示了什么?现在,您的总数将始终为negative time,因为您正在从较早的日期中减去较晚的日期。 10:00 - 16:00 = -6:0016:00-10:00 = 6
  • 我的问题是我从未计算过的代码中的错误。 “在 mscorlib.dll 中发生了 'System.FormatException' 类型的未处理异常附加信息:字符串未被识别为有效的 DateTime。”

标签: .net vb.net datetime datagridview


【解决方案1】:

您可能希望从时钟输出中减去输入时钟(否则您可能会得到负的时间跨度)。

DataGridView 单元格的值是一个对象,因此您可能需要一个 .ToString 才能将其解析为 DateTime。

您可能还需要一个.ToString():在设置Value

    Dim clockIn = DateTime.Parse(row.Cells("clock in").Value.ToString())
    Dim clockOut = DateTime.Parse(row.Cells("clock out").Value.ToString())
    Dim total = clockOut - clockIn
    row.Cells("total").Value = total.ToString()

如果 DateTime.Parse 不起作用,您将不得不查看 DateTime.ParseExact

【讨论】:

  • 此错误仍然显示“在 mscorlib.dll 中发生类型为 'System.FormatException' 的未处理异常附加信息:字符串未被识别为有效的 DateTime。”
  • 哦 - 现在你已经发布了另一个问题的错误
猜你喜欢
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
相关资源
最近更新 更多