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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18505431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:50:52  来源:igfitidea点击:

How to move to another column in datagridview using Enter in vb.net

vb.netdatagridviewkeypress

提问by Matthew

I have search this one, and most of the answer are in C# and I have try to convert it to VB.NET and do a work around but I cannot find the right code.

我搜索了这个,大部分答案都在 C# 中,我尝试将其转换为 VB.NET 并进行了解决,但我找不到正确的代码。

What I want is when the user press Enter, it will move to the next column, if the column in that row is last, then it will go down to the second row of the first column.

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

Thank you.

谢谢你。

EDIT:

编辑:

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

This code is working but for columns that are not editing, if I am editing in a columns, its not working and this is the error: Current cell cannot be set to an invisible cell.

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

回答by sevdalone

this should do the trick

这应该可以解决问题

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

and this will address the "edit" problem mentioned.

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

   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

回答by E-r Gabriel Doronila

to be safe.. you should get your "current" column index when your cell changed.. then you can increment from that.. this will also sort out the problem with the "editing"

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

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

回答by omar wahab

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