Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

Problem with moving cursor to desired cell in datagrigview

$
0
0
Hello my friends,

Looking for your help againg.

The case: I have a 6 columns datagridview (dgv) which is filled by the user. I need, with ENTER KEY, the cursor moves down, just as it usually does in dgv . But, when it reaches the last row, the need is it goes to the top cell in the next column. On the other hand, if it is in the last cell of the last colmumn (5), it beeps and stay in that cell, until the user checks the data and clicks a button to accept it.

The code: I did a little code with a help of Tengkorak in "[RESOLVED] Datagridview: move to column 4 but instead to column 5" which I found in VB Forum. http://www.vbforums.com/showthread.p...ad-to-column-5

The problems: 1. When cursor is in the last row of any column, a double Enter Key down is needed to have it moved to the top cell of the next column. That leads to a trouble when introducing data to the dgv, because data is misplaced.

2. It does not beep when it is in the last row of the last column, eventhough cursor stays there.


Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

Dim cRow As Integer = DataGridView1.CurrentCell.RowIndex
Dim cCol As Integer = DataGridView1.CurrentCell.ColumnIndex

If e.KeyCode = Keys.Enter Then
SendKeys.Send("{UP}")
SendKeys.Send("{RIGHT}")


If DataGridView1.RowCount = DataGridView1.CurrentCell.RowIndex + 1 Then
DataGridView1.Rows(0).Selected = True
MoveCell(0, cCol)


If cCol = 5 And DataGridView1.RowCount = DataGridView1.CurrentCell.RowIndex + 1 Then
SendKeys.Send("{DOWN}") 'The way I found to have the cursor in that cell
MsgBox("")
Beep()
MoveCell(DataGridView1.RowCount - 1, 5)
Exit Sub
End If
End If
End If
End Sub
Sub MoveCell(ByVal crow As Integer, ByVal cCol As Integer)
If crow <= 0 Then crow = 0
DataGridView1.CurrentCell.Selected = False
DataGridView1.Rows(crow).Cells(cCol).Selected = True
DataGridView1.CurrentCell = DataGridView1.SelectedCells(0)
DataGridView1.Refresh()
End Sub

Any help will be appreciated.

Nelson

Viewing all articles
Browse latest Browse all 27513

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>