Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Grid Navigation

Status
Not open for further replies.

saimagulse

Programmer
Aug 5, 2006
9
PK
Hi All
I want to do navigation through enter key in DataGrid. if user has entered a wrong accountno (Which doest not exist in my database) in first cell of grid then input box window will open and user write accountno this will be validated with accountno in database and if it is correct it should be written in first cell of current row and focus should be moved to next cell of current row. I write code for this but problem is that when user enter wrong code in cell and then press enter input box open where user enter code and this is verified and then written in cell of current row(this work fine) but focus moved to first cell of next row instead of next cell of current row. I want that focus should remain in current row. How can i do this .Please help me as soon as possible
Here is my code you can build a dg and chekc its navigation
Public Class DV
Private vEditingRow As Integer = -1
Dim vIsSet As Boolean
Dim vSQLStr As String
Dim objDataReader As OleDb.OleDbDataReader
Dim objCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand



Private Sub DG_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG.CellClick
Try
Select Case DG.Columns(e.ColumnIndex).Name
Case "BtnSelAccount" '---ButtonClick
Call FunSelAccount("")
End Select
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'--- Save the current row number.
Private Sub DG_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DG.EditingControlShowing
vEditingRow = DG.CurrentRow.Index
End Sub
'--- Ignore Return keys.
Private Sub DG_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DG.KeyDown
Try
If e.KeyCode = Keys.Return Then
Dim cur_cell As DataGridViewCell = DG.CurrentCell
Dim vCurRow As DataGridViewRow = DG.CurrentRow
Dim col As Integer = cur_cell.ColumnIndex
If col = 0 Then
col = (col + 3) Mod DG.Columns.Count
Else
col = (col + 1) Mod DG.Columns.Count
End If
cur_cell = DG.CurrentRow.Cells(col)
DG.CurrentCell = cur_cell
DG.CurrentCell.Selected = True
'---To Move To Next Row
If col = 0 Then
DG.CurrentCell = DG.Rows(DG.CurrentRow.Index + 1).Cells(0)
DG.CurrentCell.Selected = True
End If
e.Handled = True

End If
Catch ex As Exception
MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub

'--- If we just finished editing, restore the current row number.
Private Sub DG_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DG.SelectionChanged
If vIsSet = True Then vIsSet = False : Exit Sub
If vEditingRow >= 0 Then
Dim new_row As Integer = vEditingRow
vEditingRow = -1
DG.CurrentCell = DG.Rows(new_row).Cells(DG.CurrentCell.ColumnIndex)
End If
End Sub

Private Sub DG_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DG.CellValidating
Try
Select Case DG.Columns(e.ColumnIndex).Name
Case "AccountNo"
If DG.CurrentCell.IsInEditMode = True Then
DG.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
End If
If DG.CurrentCell.Value Is Nothing Then Exit Sub
If FunSelAccount(CType(DG.CurrentCell.Value, String)) = False Then
e.Cancel = Not FunSelAccount("")
Else
DG.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)

End If
vIsSet = True
End Select
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub

Private Function FunSelAccount(ByVal InAccNo As String) As Boolean
Try
If InAccNo = "" Then
InAccNo = InputBox("Enter AccNo", "AccountNo")
End If
objCmd.CommandText = "Select AccountNo,AccountName From ChartOfAccounts Where IsDetailed=1 And AccountNo='" & InAccNo & "'"
objCmd.Connection = CN
objDataReader = objCmd.ExecuteReader
If objDataReader.Read Then
DG.CurrentRow.Cells(0).Value = objDataReader("AccountNo").ToString
DG.CurrentRow.Cells(2).Value = objDataReader("AccountName").ToString
FunSelAccount = True
Else
DG.CurrentRow.Cells(2).Value = ""
End If
objDataReader.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Function



End Class




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top