none
VB.NET DataGridView - Set Selected Row Items To TextBoxes on enter key

    Question

  • Good day Tech Gurus,

    l am working on a point of sale system. l need help on how to display DataGridView in text boxes using enter key. The following code works well when using a mouse. l need an equivalent using enter key as most customers want to work with the key board. Here is the code working with a mouse:

        Private Sub dgViewPOS_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgViewPOS.MouseUp
        Dim hit As DataGridView.HitTestInfo = Me.dgViewPOS.HitTest(e.X, e.Y)
      
            If hit.Type = DataGridViewHitTestType.Cell Then
                Try
                    Me.dgViewPOS.ClearSelection()
                    Me.dgViewPOS.Rows(hit.RowIndex).Selected = True
                    txtProPurCode.Text = Me.dgViewPOS.SelectedCells(0).Value
                    txtProductName.Text = Me.dgViewPOS.SelectedCells(1).Value
                    txtStockQty.Text = Me.dgViewPOS.SelectedCells(2).Value
                    txtPrice.Text = Me.dgViewPOS.SelectedCells(3).Value
                    txtVat.Text = Me.dgViewPOS.SelectedCells(4).Value
                    txtTotalPrice.Text = Me.dgViewPOS.SelectedCells(5).Value

                Catch ex As Exception
                End Try
            End If

    End Sub

    On any other event, l get errors:

    'X' is not a member of 'System.Windows.Forms.KeyEventArgs'.
    'Y' is not a member of 'System.Windows.Forms.KeyEventArgs'.

    This is emanating from the line:
    Dim hit As DataGridView.HitTestInfo = Me.dgViewPOS.HitTest(e.X, e.Y)

    l also need assistance on how to move from one row to another using up and down keys so that l then set the data on selected rows to text boxes by use of enter key.

    The following code demonstrates how l am populating the datagrid:

    Private Sub txtProName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtProName.KeyPress
            Dim clsPOS As New SalesAndIssues("POS_GUI")
            Dim ds As DataSet = Nothing

            Try
                ds = clsPOS.getLikeProductDetails(txtProName.Text)
                dgViewPOS.ClearSelection()
                dgViewPOS.DataSource = ds.Tables(0).DefaultView
                dgViewPOS.Refresh()

            Catch ex As Exception
            End Try
        End Sub

    • Edited by tangozop Tuesday, March 14, 2017 4:22 PM adding more information
    Tuesday, March 14, 2017 11:38 AM

Answers

  • Thank you so much guys. sorry for taking too long to respond. l might not have implemented exactly what you posted here but your posts did help me arrive at a solution. l did the following:

    1) Created a function to view POS

     Public Sub ViewPOS()
            Try
                txtProPurCode.Text = Me.dgViewPOS.CurrentRow.Cells(0).Value
                txtProductName.Text = Me.dgViewPOS.CurrentRow.Cells(1).Value
                txtStockQty.Text = Me.dgViewPOS.CurrentRow.Cells(3).Value
                txtStockReOrder.Text = Me.dgViewPOS.CurrentRow.Cells(4).Value
                txtPrice.Text = Me.dgViewPOS.CurrentRow.Cells(5).Value
            Catch ex As Exception

            End Try

        End Sub

    2) Created a keys module function and called the function ViewPOS as follows:

      Case Keys.Enter

        If Keys.Enter And dgViewPOS.Focus = True Then

                        ViewPOS()
                        txtQty.Visible = True
                        txtQty.Focus()
                        dgViewPOS.Visible = False
                        Exit Sub
                    End If

    This worked wonders for me. Thanks a lot.

    • Marked as answer by tangozop Friday, July 28, 2017 8:42 AM
    Friday, July 28, 2017 8:41 AM

All replies

  • In WPF I think you have a number of options. On key down, before key down, key up, and a number of preview events. It sounds to me like key down, or preview key down, would work nicely. Then you would need to test which key is pressed, and go from there.
    • Edited by George-Frias Tuesday, March 14, 2017 12:00 PM Grammar
    Tuesday, March 14, 2017 11:59 AM
  • I would use the SelectionChanged Event. It fires regardless of whether a row is selected by way of a mouse click or a keystroke.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, March 14, 2017 1:07 PM
  • Hello,

    A good method to work with data in a DataGridView is to set the DataSource to say a DataTable, DataSet or other source. Then to work the data you can cast the current row to the source e.g. in this case I've read data from a database into a DataTable via a class used solely for data operations. Similarly this can be done with a TableAdapter (has a DataSet and BindingSource)

    Public Class Form4
        Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) _
            Handles DataGridView1.KeyDown
    
            If e.KeyData = Keys.Enter Then
                e.Handled = True
                Dim currentRow As DataRow = CType(DataGridView1.DataSource, DataTable) _
                    .Rows(DataGridView1.CurrentRow.Index)
                MessageBox.Show(currentRow.Field(Of String)("CompanyName"))
            End If
        End Sub
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New DatabaseOperations
            ops.GetCustomerData()
            Dim dt As DataTable = ops.DataTable
            DataGridView1.DataSource = dt
        End Sub
    End Class

    Going with a BindingSource

    Public Class Form4
        Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) _
            Handles DataGridView1.KeyDown
    
            If e.KeyData = Keys.Enter Then
                e.Handled = True
                Dim currentRow As DataRow = CType(BindingSource1.Current, DataRowView).Row
                MessageBox.Show(currentRow.Field(Of String)("CompanyName"))
            End If
        End Sub
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New DatabaseOperations
            ops.GetCustomerData()
            BindingSource1.DataSource = ops.DataTable
            DataGridView1.DataSource = BindingSource1
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 14, 2017 1:20 PM
    Moderator
  • Ok, on key down gives an error message:  'X' and 'Y' is not a member of System.Windows.Forms.KeyEventArgs.   This is emanating from the line :

    Dim hit As DataGridView.HitTestInfo = Me.dgViewPOS.HitTest(e.X, e.Y)

    Tuesday, March 14, 2017 2:53 PM
  • This is how l am populating the DataGridView:

    Private Sub txtProName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtProName.KeyPress
            Dim clsPOS As New SalesAndIssues("POS_GUI")
            Dim ds As DataSet = Nothing

            Try
                ds = clsPOS.getLikeProductDetails(txtProName.Text)
                dgViewPOS.ClearSelection()
                dgViewPOS.DataSource = ds.Tables(0).DefaultView
                dgViewPOS.Refresh()

            Catch ex As Exception
            End Try
        End Sub

    Tuesday, March 14, 2017 2:57 PM
  • You test for the key pressed, not a mouse click in this scenario.
    Tuesday, March 14, 2017 7:56 PM
  • So dgViewPOS.DataSource = ds.Tables(0) (0 points to the first DataTable in your DataSet) is equivalent to my DataGridView1.DataSource = dt, for DefaultView it's simply a view into the DataTable. So you should be able to do what I did from there.

    Off topic, never use a try/catch without doing something in the catch.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 14, 2017 8:13 PM
    Moderator
  • Thank you so much guys. sorry for taking too long to respond. l might not have implemented exactly what you posted here but your posts did help me arrive at a solution. l did the following:

    1) Created a function to view POS

     Public Sub ViewPOS()
            Try
                txtProPurCode.Text = Me.dgViewPOS.CurrentRow.Cells(0).Value
                txtProductName.Text = Me.dgViewPOS.CurrentRow.Cells(1).Value
                txtStockQty.Text = Me.dgViewPOS.CurrentRow.Cells(3).Value
                txtStockReOrder.Text = Me.dgViewPOS.CurrentRow.Cells(4).Value
                txtPrice.Text = Me.dgViewPOS.CurrentRow.Cells(5).Value
            Catch ex As Exception

            End Try

        End Sub

    2) Created a keys module function and called the function ViewPOS as follows:

      Case Keys.Enter

        If Keys.Enter And dgViewPOS.Focus = True Then

                        ViewPOS()
                        txtQty.Visible = True
                        txtQty.Focus()
                        dgViewPOS.Visible = False
                        Exit Sub
                    End If

    This worked wonders for me. Thanks a lot.

    • Marked as answer by tangozop Friday, July 28, 2017 8:42 AM
    Friday, July 28, 2017 8:41 AM