自动在DataGridView中移动下一列

时间:2015-10-28 18:38:04

标签: vb.net datagridview

我有以下代码,我需要执行以转到下一列IF条件满足。 请帮忙。

 Dim i As Integer
    i = DataGridView1.CurrentRow.Index
    If i >= 0 Then
        Service_Reservation.TxtBoxResID.Text = DataGridView1.Item(0, i).Value
        Service_Reservation.txtBoxSerID.Text = DataGridView1.Item(1, i).Value
        Service_Reservation.DatePickerDateIn.Text = DataGridView1.Item(2, i).Value
        If DataGridView1.Item(3, i).Value Is Nothing Then
            ****
        Else
            Service_Reservation.DateTimePickerDateOut.Text = DataGridView1.Item(3, i).Value
        End If
        Service_Reservation.DateTimePickerTimeIn.Text = DataGridView1.Item(4, i).Value
        Service_Reservation.DateTimePickerTimeOut.Text = DataGridView1.Item(5, i).Value
        Service_Reservation.txtBoxQty.Text = DataGridView1.Item(6, i).Value

1 个答案:

答案 0 :(得分:0)

我认为您可能正在寻找一种简化语法的方法来测试对象并非一无是处。 .Net在某些方面添加了IsNot,避免了笨拙的编码:

    If DataGridView1.Item(3, i).Value IsNot Nothing Then
        Service_Reservation.DateTimePickerDateOut.Text = DataGridView1.Item(3, i).Value
    End If
    'clumsy way but valid
    If Not DataGridView1.Item(3, i).Value Is Nothing Then
        Service_Reservation.DateTimePickerDateOut.Text = DataGridView1.Item(3, i).Value
    End If

空的IF或ELSE部分没问题 - 有时会使代码更具可读性

    If DataGridView1.Item(3, i).Value Is Nothing Then
        ' *** probably bad data - see Fred
    Else
        Service_Reservation.DateTimePickerDateOut.Text = DataGridView1.Item(3, i).Value
    End If

请注意,您可以使用列名来寻址单元格(除非您使用IDE创建数据对象。)

    If DataGridView1.Item("Reservation_Date", i).Value Is Nothing Then
        ****
    Else
        Service_Reservation.DateTimePickerDateOut.Text = DataGridView1.Item(3, i).Value
    End If