将光标移动到DataGridView中的另一个单元格-VB.NET

时间:2018-12-01 17:28:06

标签: vb.net datagridview

我有一个DataGridView和6个column

Column1 = "Product ID"   //ReadOnly False
COlumn2 = "Product Name" //ReadOnly True - Auto Fill After Product ID Entered
COlumn3 = "Product Info" //ReadOnly True - Auto Fill After Product ID Entered
Column4 = "Price"        //ReadOnly False
Column5 = "Qty"          //ReadOnly False
Column6 = "Sub Total"    //ReadOnly True - Auto Fill From Price*Qty

我如何仅使用ReadOnly设置为False的单元格上的选项卡?我的意思是,如果我使用有铅的输入产品ID并按Enter键,则光标/焦点将移至Column4,因此在{{1 }},然后在Column5中再次按Enter键后添加新行。

Allready已尝试使用其他代码,但仅适用于所有单元格而不是特定单元格。

2 个答案:

答案 0 :(得分:0)

http://www.vbforums.com/showthread.php?735207-DAtaGridView-move-to-next-column-automatically

您尝试过此网站吗?抱歉,我无法在您的评论部分发布,因为它要求50信誉。

答案 1 :(得分:0)

到目前为止,我已使用Enter键将其移动到另一个单元格中。使用Arsalan Tamiz Blog的代码。

Dim celWasEndEdit As DataGridViewCell
Private _EnterMoveNext As Boolean = True
<System.ComponentModel.DefaultValue(True)>
    Public Property OnEnterKeyMoveNext() As Boolean
        Get
            Return _EnterMoveNext
        End Get
        Set(ByVal value As Boolean)
            _EnterMoveNext = value
        End Set
    End Property
Private Sub DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        'if Enter Move Next should work andalso mouse button was NOT down
        'we are checking mouse buttons because if select was changed
        'by Mouse then we will NOT do our Enter Move Next
        If _EnterMoveNext AndAlso MouseButtons = 0 Then
            'if selection is changed after Cell Editing
            If celWasEndEdit IsNot Nothing AndAlso DataGridView1.CurrentCell IsNot Nothing Then
                'if we are currently in the next line of last edit cell
                If DataGridView1.CurrentCell.RowIndex = celWasEndEdit.RowIndex + 1 AndAlso DataGridView1.CurrentCell.ColumnIndex = celWasEndEdit.ColumnIndex Then
                    Dim iColNew As Integer
                    Dim iRowNew As Integer

                    'if we at the last column
                    If celWasEndEdit.ColumnIndex >= DataGridView1.ColumnCount - 2 Then
                        iColNew = 0                         'move to first column
                        iRowNew = DataGridView1.CurrentCell.RowIndex   'and move to next row
                    Else 'else it means we are NOT at the last column move to next column
                        iColNew = celWasEndEdit.ColumnIndex + 1
                        'but row should remain same
                        iRowNew = celWasEndEdit.RowIndex
                    End If
                    DataGridView1.CurrentCell = DataGridView1(iColNew, iRowNew)   ' ok set the current column
                End If
            End If
            celWasEndEdit = Nothing                      ' reset the cell end edit
        End If
    End Sub

并绕过。我使用了它(但是我不认为它是完美的选择,因为如果我在第4列上按向左箭头移动到第0列,焦点就不能移到它,也许是因为我把Keys.Sender放在了右边) >

Private Sub DataGridView1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        If DataGridView1.Columns(e.ColumnIndex).ReadOnly = True Then
            SendKeys.Send("{Right}")
        End If
    End Sub