如何从多个datagridviews中减去和比较值?

时间:2014-06-24 05:05:14

标签: vb.net datagridview

比较2个datagridviews的值,值将为datetime。

第1步

我使用if语句匹配列(ID)列(日期范围)中的值,如果正确,我会将它们转到新列。 [我得到了正确的结果]

第2步

我尝试添加更多条件来比较列(时间) [我的预期结果都不正确(“No-in”)]

任何人都可以帮助我或就此分享意见吗?

Dim storageEarlyin As String
Dim storagelatein As String
'Dim storatimetemp As DateTime = "12:00:00 PM"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim count As Integer = DataGridView1.Rows.Count

    For i = 0 To (count - 1)
        Dim clockeddate As String = Convert.ToDateTime(DataGridView1.Rows(i).Cells(1).Value).ToShortDateString
        Dim userid As String = DataGridView1.Rows(i).Cells(0).Value.ToString()
        Dim count2 As Integer = DataGridView2.Rows.Count
        Dim clockedtime As String = Convert.ToDateTime(DataGridView1.Rows(i).Cells(1).Value).ToShortTimeString

        For i2 = 0 To (count2 - 1)

            Dim daterange As String = Convert.ToDateTime(DataGridView2.Rows(i2).Cells(1).Value).ToShortDateString
            Dim userid2 As String = DataGridView2.Rows(i2).Cells(0).Value.ToString()
            Dim workstart As String = Convert.ToDateTime(DataGridView2.Rows(i2).Cells(5).Value).ToShortTimeString
            Dim ClockAvailableStart As String = Convert.ToDateTime(DataGridView2.Rows(i2).Cells(3).Value).ToShortTimeString
            Dim workend As String = Convert.ToDateTime(DataGridView2.Rows(i2).Cells(6).Value).ToShortTimeString

            If userid = userid2 And clockeddate = daterange Then
                If clockedtime <= workstart And clockedtime >= ClockAvailableStart Then
                    storageEarlyin = DataGridView1.Rows(i).Cells(1).Value.ToShortTimeString
                    Me.DataGridView2.Rows(i2).Cells(7).Value = storageEarlyin
                Else
                    storageEarlyin = "No-in"
                    Me.DataGridView2.Rows(i2).Cells(7).Value = storageEarlyin
                End If

                If clockedtime > workstart And clockedtime < workend Then
                    storagelatein = DataGridView1.Rows(i).Cells(1).Value.ToShortTimeString
                    Me.DataGridView2.Rows(i2).Cells(8).Value = storagelatein
                Else
                    storagelatein = "No-in"
                    Me.DataGridView2.Rows(i2).Cells(8).Value = storagelatein
                End If
            End If
        Next
    Next
End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

您创建的变量clockedtime ,workstart ClockAvailableStarstring类型,因此if条件(clockedtime <= workstart And clockedtime >= ClockAvailableStart)无法正常工作,因此更改数据类型为datetime,如

 Dim clockedtime As DateTime= 
 Convert.ToDateTime(DataGridView1.Rows(i).Cells(1).Value).ToShortTimeString

 Dim workstart As DateTime= 
 Convert.ToDateTime(DataGridView2.Rows(i2).Cells(5).Value).ToShortTimeString

 Dim ClockAvailableStart As DateTime= 
 Convert.ToDateTime(DataGridView2.Rows(i2).Cells(3).Value).ToShortTimeString

之后它正常工作