我有1个datagridview 3列
如果用户在StartLoading和FinishLoading Columns中输入日期和时间,则从TotalLoadingHours列开始显示从开始到结束的总小时数。
我的问题是,如果用户仅在StartLoading Column中输入日期和时间,则总会出现错误 - "字符串未被识别为有效的DateTime"。
感谢您的帮助。以下是我的代码。
Dim StartLoading As New System.DateTime
Dim FinishLoading As New System.DateTime
For x As Integer = 0 To LoadingStatusDataGridview.Rows.Count - 2
Dim StartLoadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString()
StartLoading = DateTime.Parse(StartLoadingvalue)
Dim FinishLoadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString()
FinishLoading = DateTime.Parse(FinishLoadingvalue)
Dim TotalLoadingHours1 As TimeSpan = (FinishLoading - StartLoading)
String.Format("{0:00}:{1:00}:{2:00}", TotalLoadingHours1.TotalHours, TotalLoadingHours1.Minutes, TotalLoadingHours1.Seconds)
Dim TotalLoadingHours2 As TimeSpan = (DateTime.Now - StartLoading)
String.Format("{0:00}:{1:00}:{2:00}", TotalLoadingHours2.TotalHours, TotalLoadingHours2.Minutes, TotalLoadingHours2.Seconds)
If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value() Is Nothing Then
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = Nothing
ElseIf IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value() Is Nothing Then
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = TotalLoadingHours2
Else
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = TotalLoadingHours1
End If
Next
End Sub
答案 0 :(得分:0)
尝试格式化单元格,如果FinishLoading或StartLoading为空,则不计算结果:
dataGridView1.Columns(0).DefaultCellStyle.Format = "MM/dd/yyyy"
dataGridView1.Columns(1).DefaultCellStyle.Format = "MM/dd/yyyy"
答案 1 :(得分:0)
现在正在使用下面的代码..
Dim startloading As New System.DateTime
Dim finishloading As New System.DateTime
For x As Integer = 0 To LoadingStatusDataGridview.Rows.Count - 2
Dim startloadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString()
If Not IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value) AndAlso LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString.Length <> 0 Then
startloading = DateTime.Parse(startloadingvalue)
End If
Dim finishloadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString()
If Not IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value) AndAlso LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString.Length <> 0 Then
finishloading = DateTime.Parse(finishloadingvalue)
End If
If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value() Is Nothing OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString.Trim() = "" Then
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = Nothing
End If
If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value() Is Nothing OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString.Trim() = "" Then
Dim LoadingHours2 As TimeSpan = (DateTime.Now - startloading)
String.Format("{0:00}:{1:00}:{2:00}", LoadingHours2.TotalHours, LoadingHours2.Minutes, LoadingHours2.Seconds)
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = LoadingHours2
Else
Dim LoadingHours1 As TimeSpan = (finishloading - startloading)
String.Format("{0:00}:{1:00}:{2:00}", LoadingHours1.TotalHours, LoadingHours1.Minutes, LoadingHours1.Seconds)
LoadingStatusDataGridview.Rows(x).Cells(3).Value() = LoadingHours1
End If
Next