הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, March 14, 2017 11:38 AM
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
All replies (6)
Friday, July 28, 2017 8:41 AM ✅Answered
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.
Tuesday, March 14, 2017 1:07 PM
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:20 PM | 1 vote
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
Tuesday, March 14, 2017 2:53 PM
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:57 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 8:13 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