Question stopping going next row in datagriview

hazoutd

Member
Joined
Feb 25, 2013
Messages
5
Programming Experience
1-3
hi
i have to stop , using tabs key, when the focus is in the last column going to the next row when i m in editing mode ,in datagridview
i try EditingControlShowing this event with keys.tab
please i need help
dave
 
You could create a class that inherits DataGridView and override ProcessDialogKey method.
 
This will do it for you

Public Class Form1
    Private lastcol As Boolean = False


    Private Sub DGVCellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgv1.CellEnter

        If e.ColumnIndex = dgv1.Columns.Count - 1 Then
            lastcol = True
        Else
            lastcol = False
        End If
    End Sub



    Private Sub FormKeyDown(sender As Object, e As KeyEventArgs) Handles dgv1.KeyDown
        If lastcol = True Then
            e.Handled = True

        End If
    End Sub
End Class


 
Last edited:
hi john
how can i use the inherits of datagriview
must i use this inherits in a local class who is winforms?
the datagridview is in a forms
i m writtîg in vb.net
have i an example?
thanks a lot for tour answers
lotok i ll try it tomorow morning
thanks
 
This will do it for you

Public Class Form1
Private lastcol As Boolean = False


Private Sub DGVCellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgv1.CellEnter

If e.ColumnIndex = dgv1.Columns.Count - 1 Then
lastcol = True
Else
lastcol = False
End If
End Sub



Private Sub FormKeyDown(sender As Object, e As KeyEventArgs) Handles dgv1.KeyDown
If lastcol = True Then
e.Handled = True

End If
End Sub
End Class



hi this not do what i want it is goint to the next row
perhaps i dont explain myself good
what i need is that:
when i m in editing mode the user can navigate just in the current row till he click in arrow up or down or page up/down
thanks for yours replies
dave
 
Apologies, it was late and I somehow missed the fact we were discussing in edit mode. This only prevents the tab on last column in view mode.
I will have a wee play later and see if I can enhance it to include editmode
 
To do it without Inheriting I had to replace the Tab key with another. I chose Escape which just exits the edit mode and leaves the end column highlghted.
I used a DataGridView on my form called dgv2



Public Class Form1

    Private Property ColNo() As Integer
    
    Private Sub dgv2_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgv2.CellEnter
        'track column index
        ColNo = e.ColumnIndex
    End Sub

    Private Sub dgv2_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv2.EditingControlShowing
        'handle key event in edit mode
        Dim txtControl = DirectCast(e.Control, TextBox)
        AddHandler txtControl.PreviewKeyDown, AddressOf PreviewKeyDownMethod
    End Sub
    
Private Sub PreviewKeyDownMethod(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs)
        'check for tab key
        If e.KeyCode = Keys.Tab Then
            CancelKey()
        End If

        Dim txtControl = DirectCast(sender, TextBox)
        RemoveHandler txtControl.PreviewKeyDown, AddressOf PreviewKeyDownMethod
    End Sub
    
Private Sub CancelKey()
        'replace tab with escape if last column
        If ColNo = dgv2.Columns.Count - 1 Then
            Windows.Forms.SendKeys.SendWait("{ESC}")
        End If
    End Sub

End Class


 
Last edited:
You're working very hard to avoid a simple solution, Lotok. That code also has a memory leak, AddHandler without RemoveHandler.
 
(Solved) GOING TO THE NEXT OR PREVIOUS ROW

i found the solution
with tab and shift tab i try it and it is working

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
Dim keyPressed As Keys = CType(msg.WParam.ToInt32(), Keys)
If (keyPressed = Keys.Tab) Then ' And ModifierKeys = Keys.Shift) Then
If (DataGridView1.CurrentRow IsNot Nothing AndAlso DataGridView1.CurrentCell IsNot Nothing) Then
If (ModifierKeys = Keys.Shift) Then
If (DataGridView1.CurrentCell.ColumnIndex > 0) Then
DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells(DataGridView1.CurrentCell.ColumnIndex - 1)
End If
Else
If (DataGridView1.CurrentCell.ColumnIndex < DataGridView1.ColumnCount - 1) Then
DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells(DataGridView1.CurrentCell.ColumnIndex + 1)
End If
End If
End If
End If
Return (keyPressed = Keys.Tab)
End Function

thanks a lot for all of you

dave
 
Thanks for the catch on the RemoveHandler, Added it to the called method. I just felt like seeing if it could be done without inheritance. Not to say it was the better solution, just another option.
I looked at inheriting the DataGridView to be honest and didn't see a faster way, I didn't see any event to overwrite for a quick fix, but if you have 10 mins would be great to see the inheritance method for learning sake.
 
I didn't see any event to overwrite for a quick fix, but if you have 10 mins would be great to see the inheritance method for learning sake.
Well, I could grant you one more minute ;)
Public Class DataGridViewEx
    Inherits DataGridView

    Protected Overrides Function ProcessDialogKey(keyData As System.Windows.Forms.Keys) As Boolean
        If Me.CurrentCellAddress.X = Me.ColumnCount - 1 AndAlso keyData = Keys.Tab Then
            Me.EndEdit()
            Return True
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function

End Class
 
Back
Top