• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1164
  • Last Modified:

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

0
ekenman
Asked:
ekenman
  • 7
  • 3
1 Solution
 
ekenmanAuthor Commented:
(I've tried a timer.... it works, but naturally I'd like to avoid it....)
0
 
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

Tomas
0
 
Priest04Commented:
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

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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!
0
 
Priest04Commented:
>> 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)

0
 
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

0
 
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 :)
0
 
Priest04Commented:
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

0
 
ekenmanAuthor Commented:
OK, I'll try this
0
 
ekenmanAuthor Commented:
Thank you so much! That does it!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now