[RESOLVED] Datagridview: move to column 4 but instead to column 5
I want to move the cursor from column 1 to column 4 but what happened instead the cursor moves to column 5, this happens when I use the enter key event. but run well if I double click.
The following description of the program:
there are 7 columns of code, item description, unit, price, qty, discount, item amount
code when key enter press :
vb.net Code:
Private Sub dg_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyUp
If e.KeyData = Keys.Enter Then
Dim cRow As Integer = dg.CurrentCell.RowIndex
Dim cCol As Integer = dg.CurrentCell.ColumnIndex
Select Case cCol
Case 0, 1, 2
LoadProduct()
MoveCell(cRow, 4)
end select
end if
end sub
and code when double click at datagridview.
vb.net Code:
Private Sub dg_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellDoubleClick
Select Case e.ColumnIndex
Case 0, 1
LoadProduct()
MoveCell(cRow, 4)
End Select
End Sub
at LoadProduct() method program load form dialog to get product list and then choose one of them.
vb.net Code:
'move cursor to crow & ccol cell
Sub MoveCell(ByVal crow As Integer, ByVal cCol As Integer)
If crow <= 0 Then crow = 0
dg.CurrentCell.Selected = False
dg.Rows(crow).Cells(cCol).Selected = True
dg.CurrentCell = dg.SelectedCells(0)
dg.Refresh()
End Sub
I have some ways but still could.
thank you
Re: Datagridview: move to column 4 but instead to column 5
Like every collection in .NET, DataGridView.Columns is zero-based. The first column is index 0, the fourth column is index 3.
Re: Datagridview: move to column 4 but instead to column 5
agree, the index of the column in the grid starting from 0.
I want my code to move from column to column qty but what happened instead move the cursor to the column discount
Re: Datagridview: move to column 4 but instead to column 5
Can you not just do this?
vb.net Code:
myDataGridView.CurrentCell = myDataGridView(desiredColumnIndexOrName, e.RowIndex)