1

我已经搜索了这个,大多数答案都在 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

此代码有效,但对于未编辑的列,如果我在列中进行编辑,则它不起作用,这是错误:当前单元格不能设置为不可见的单元格。

4

3 回答 3

5

这应该可以解决问题

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
于 2013-08-29T08:50:39.927 回答
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
于 2013-08-29T09:14:21.353 回答
0

I think ....

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
于 2014-05-27T22:04:27.733 回答