Controlling the taborder in a DataGridView

I'm working on an orderform with order-rows bound in a DataGridView. My first Column contains ProductNo. If the User fills out the ProductNo, Columns with description, price etc are automatically filled out. These I'd like the user to be able to skip if they've filled out the first column correctly. Under what Event can I do this? The datatable lookup is done under CellValueChanged. It seems wherever I put my code it is overridden by the DGVs own procedures.

If I put a button on the form and press it, my attached code works like a charm.

dgvOrderRow.CurrentCell = dgvOrderRow.Rows(0).Cells("txtQuantity")

Open in new window

Who is Participating?
Priest04Connect With a Mentor Commented:
Ok, I see what you mean. In order to achieve what you want, you would need to override default behavior of Enter key. This will require to create a custom datagridview control and override ProcessDialogKey to handle enter key. Yo will need to use CustomDataGridView instead of DataGridView control whenever you need this functionality.
Public Class dgv
    Inherits DataGridView
    Protected Overloads Overrides Function ProcessDialogKey(ByVal KeyData As Keys) As Boolean
        Dim key As Keys = KeyData And Keys.KeyCode
        Select Case key
            Case Keys.Enter
                ' simulate that CTRL+ENTER is pressed, so control will not jump to cell bellow
                KeyData = KeyData Or Keys.Control
                Exit Select
        End Select
        Return MyBase.ProcessDialogKey(KeyData)
    End Function
End Class

Open in new window

ekenmanAuthor Commented:
(I've tried a timer.... it works, but naturally I'd like to avoid it....)
ekenmanAuthor Commented:
OK, I'm off to a meditation retreat for the rest of the week (wich I really need since I really can't solve this). I won't be able to test any solutions but I'll leave the question open and hope that it's not impossible without writing a grid my self...

Best Regards

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

I would make the datagridview unbound, then in a DataGridView CellEndEdit event, I would do the following:

sub dfb_CellEndEdit(......)
    if (e.ColumnIndex = 0) then
        ' do a search on a datatable (or dataview) for the ProductNo the user has typed in
        ' when record is found, manually update other cells with the values from the found reocrd
    end if
end sub

Open in new window

ekenmanAuthor Commented:
Hi Priest04,

There is no difference with bound or unbound datagrids. I've tried your solution but it is the same problem that if I set the current cell in the CellEnddit event, some event that occurs after that sets the current cell to the next one.

Thanks anyway!
>> There is no difference with bound or unbound datagrids.

If you say so.

Place one datagridview on the form, add 3 columns manually, in the form load event set

dataGridView.RowCount = 5;

add this code to the datagridview_CellEndEdit()

dataGridView1.CurrentCell = dataGridView1[1, 1];

and it will always move focus to cell (1, 1)

ekenmanAuthor Commented:
Well, I tried that too... Although I'm running VB so the Syntax is a little bit different.

On an empty form I place a DataGridView. The instance is then called DataGridView1.

I've attached the code that is in the form. It doesn't work for me. I'm alowing adding, editing and deleting. Could this be the problem?
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        DataGridView1.CurrentCell = DataGridView1(1, 1)
    End Sub
    Private Sub frmPrintInvoice_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DataGridView1.Columns.Add("Column1", "First")
        DataGridView1.Columns.Add("Column2", "Second")
        DataGridView1.Columns.Add("Column3", "Third")
        DataGridView1.RowCount = 5
    End Sub

Open in new window

ekenmanAuthor Commented:
And Priest04, about the difference with bound and unbound, I should have put it like this: as far as I can see, there is no difference when it comes to this issue... There are probably a great many differenses :)
ekenmanAuthor Commented:
OK, I'll try this
ekenmanAuthor Commented:
Thank you so much! That does it!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.