如何使用Enter in vb.net移动到datagridview中的另一列

时间:2013-08-29 08:07:25

标签: vb.net datagridview keypress

我已经搜索了这个,大部分答案都在C#中,我试图将其转换为VB.NET并做一些解决方法,但我找不到合适的代码。

我想要的是当用户按Enter键时,它将移动到下一列,如果该行中的列是最后一列,那么它将下移到第一列的第二行。

谢谢。

修改

If e.KeyCode = Keys.Enter Then
        e.Handled = True
        With dvJOBranch
            Dim i As Integer = .CurrentCell.ColumnIndex + 1
            .CurrentCell = .CurrentRow.Cells(i)
        End With
    End If

此代码正在运行,但对于未编辑的列,如果我在列中进行编辑,则其无法正常工作,这是错误:当前单元格无法设置为不可见的单元格。 < / p>

3 个答案:

答案 0 :(得分:4)

这应该可以解决问题

Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                Dim iCol = DataGridView1.CurrentCell.ColumnIndex
                Dim iRow = DataGridView1.CurrentCell.RowIndex
                If iCol = DataGridView1.Columns.Count - 1 Then
                    If iRow < DataGridView1.Rows.Count - 1 Then
                        DataGridView1.CurrentCell = DataGridView1(0, iRow + 1)
                    End If
                Else
                    DataGridView1.CurrentCell = DataGridView1(iCol + 1, iRow)
                End If
            End If
        End Sub

这将解决上面提到的“编辑”问题。

   Private Sub DataGridView1_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Dim iCol = DataGridView1.CurrentCell.ColumnIndex
        Dim iRow = DataGridView1.CurrentCell.RowIndex
        If iCol = DataGridView1.Columns.Count - 1 Then
            If iRow < DataGridView1.Rows.Count - 1 Then
                DataGridView1.CurrentCell = DataGridView1(0, iRow + 1)
            End If
        Else
            If iRow < DataGridView1.Rows.Count - 1 Then
                SendKeys.Send("{up}")
            End If
            DataGridView1.CurrentCell = DataGridView1(iCol + 1, iRow)
            End If
    End Sub

答案 1 :(得分:1)

要安全..当你的单元格改变时你应该得到你的“当前”列索引..然后你可以从那里增加..这也将解决“编辑”的问题

Public curcol, currow As Integer

Private Sub DataGridView2_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView2.CurrentCellChanged
    Try
        curcol = DataGridView2.CurrentCell.ColumnIndex
        currow = DataGridView2.CurrentCell.RowIndex
    Catch ex As Exception
        curcol = 0
        currow = 0
    End Try
End Sub

Private Sub DataGridView2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView2.KeyDown
    Select Case e.KeyCode
        Case Keys.Enter
            DataGridView2.ClearSelection()
            Try
                If curcol = DataGridView1.Columns.Count - 1 Then
                    If currow < DataGridView2.Rows.Count - 1 Then
                        DataGridView2.CurrentCell = DataGridView2(0, currow + 1)
                    End If
                Else
                    DataGridView2.CurrentCell = DataGridView2(curcol + 1, currow)
                End If
            Catch ex As Exception
                Exit Try
            End Try
    End Select
End Sub

答案 2 :(得分:0)

我想......

Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler e.Control.KeyDown, AddressOf cell_KeyDown
End Sub

Private Sub cell_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.Enter Then
       datagridview1.currentrow.cells(datagridview1.currentcelladress.x+1).selected=true

    End If
End Sub