Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET如何在datagridview中计算迟到时间_.net_Vb.net_Datetime_Datagridview - Fatal编程技术网

VB.NET如何在datagridview中计算迟到时间

VB.NET如何在datagridview中计算迟到时间,.net,vb.net,datetime,datagridview,.net,Vb.net,Datetime,Datagridview,我需要有人帮我计算迟到时间,但我的代码不起作用, 我只是举个例子,这实际上并不是在打卡\ 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

我需要有人帮我计算迟到时间,但我的代码不起作用, 我只是举个例子,这实际上并不是在打卡\

 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

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

DataGridView单元格的值是一个对象,所以在将其解析为DateTime之前,您可能需要一个.ToString

您可能还需要一个
.ToString()
:设置
值时

    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

total到底显示了什么?现在,您的总时间将始终为负时间,因为您要从较早的日期中减去较晚的日期
10:00-16:00=-6:00
16:00-10:00=6
我的问题是eror在代码中我从未计算过。mscorlib.dll中发生“System.FormatException”类型的未处理异常附加信息:字符串未被识别为有效的日期时间。此eror仍显示“mscorlib.dll中发生“System.FormatException”类型的未处理异常附加信息:字符串未被识别为有效的日期时间。”哦,现在你已经发布了一个错误,这是一个不同的问题