2016-07-26 51 views
-1

我需要帮助计算晚班时间与值班 - 时钟但我的代码将不会工作, 这实际上没有时钟输入时钟我把这只是例如。\VB.NET我可以如何计算在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 
     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

+0

究竟做总说明了什么?现在你的总数将永远是一个'负面时间',因为你正在减去之前的日期。 '10:00 - 16:00 = -6:00' '16:00-10:00 = 6' – Luke

+0

我的问题是在我从未做过的代码中计算出来的错误。 mscorlib.dll 附加信息:字符串未被识别为有效的DateTime。“未处理的异常的类型'System.FormatException'发生在mscorlib.dll中。 –

回答

0

你可能想减去时钟从时钟输出(否则你可能有一个负的时间跨度)。

DataGridView Cell的值是一个对象,所以在将它解析为DateTime之前,您可能需要一个.ToString。

您可能还需要一个.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

+0

这个错误仍然显示mscorlib.dll中出现'System.FormatException'类型的未处理异常 附加信息:字符串未被识别为有效的DateTime。 –

+0

哦 - 现在你已经发布了一个不同的问题 –