I have a DataGridView in which 1 column is editable allowing a User to enter decimal data. The problem I'm having is that on the last row when data is entered and the Enter/Return Key is pressed, the value doesn't show up until I click on another cell. Not sure why! Below is my code.
Code:
Private Sub cDGV_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles cDGV.CellLeave
Try
If Not blnLoading Then
If Not gblnOEM Then
If rowIDX = cDGV.RowCount - 1 Then
cDGV.CurrentCell.Value = cTextBox.Text
cDGV.CurrentCell.Selected = False
End If
End If
End If
Catch ex As Exception
EH.strRetVal = gfrmID & "/cDGV_CellLeave() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.strRetVal)
End Sub
'
'
'
Private Sub cDGV_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles cDGV.CellValidating
Try
EH.strRetVal = ""
colIDX = cDGV.CurrentCell.ColumnIndex
rowIDX = cDGV.CurrentCell.RowIndex
If cDGV.Item(colIDX, rowIDX).IsInEditMode Then
If Not cDGV.Item(colIDX, rowIDX).ValueType Is GetType(Decimal) Then
Dim c As Control = cDGV.EditingControl
Dim cell As DataGridViewCell = cDGV.CurrentCell
If Not Decimal.TryParse(c.Text, Nothing) Then
If Not c.Text = "" Then
EH.strRetVal = "Invalid Numeric Value!" & "~I"
e.Cancel = True
End If
Else
If gblnOEM Then
Select Case e.RowIndex
Case 0
cell.Value = Format(c.Text, c.Text) 'Don't format the Load Values
Case cDGV.RowCount - 1
If cboShuntTest.Checked Then
cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
End If
Case Else
cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
End Select
blnHasData = True
Else
Select Case e.ColumnIndex
Case 1
cell.Value = Format(c.Text, c.Text) 'Don't format the Load Values
Case 2
cell.Value = Format(c.Text, GetDecimalPlaces(c.Text))
End Select
End If
cTextBox.Text = c.Text
End If
cDGV.CancelEdit()
End If
End If
Catch ex As Exception
EH.strRetVal = gfrmID & "/dgvLoadTests_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.strRetVal)
End Sub
'
'
'
Private Sub cDGV_KeyUp(sender As Object, e As KeyEventArgs) Handles cDGV.KeyUp
Try
Dim tmpCol As Integer = 0
colIDX = cDGV.CurrentCell.ColumnIndex
rowIDX = cDGV.CurrentCell.RowIndex
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
If gblnOEM Then
If cboShuntTest.Checked Then
tmpCol = 2 'Shunt Column is added to the Grid
Else
tmpCol = 1 'Shunt Column is not added to the Grid
End If
If rowIDX < cDGV.RowCount - 1 AndAlso colIDX = cDGV.ColumnCount - tmpCol Then
cDGV.CurrentCell = cDGV(2, cDGV.CurrentRow.Index + 1)
Else
Dim CurrentCell As DataGridViewCell = cDGV.CurrentCell
colIDX = CurrentCell.ColumnIndex
If colIDX = cDGV.ColumnCount - tmpCol Then
If rowIDX = cDGV.RowCount - 1 Then
CurrentCell = cDGV(2, 1)
Else
CurrentCell = cDGV(2, rowIDX + 1)
End If
Else
CurrentCell = cDGV(colIDX, rowIDX)
End If
cDGV.CurrentCell = CurrentCell
End If
Else
Dim CurrentCell As DataGridViewCell = cDGV.CurrentCell
colIDX = CurrentCell.ColumnIndex
If colIDX = cDGV.ColumnCount - 1 Or colIDX = cDGV.ColumnCount - 2 Then
If rowIDX = cDGV.RowCount - 1 Then
'cDGV.Rows(rowIDX).Cells(colIDX).Value = InputBox("Enter a value")
'If txtFinalZero.Visible = True Then
cDGV.CurrentCell.Selected = False
' txtFinalZero.Focus()
'End If
Else
CurrentCell = cDGV(2, rowIDX)
End If
Else
CurrentCell = cDGV(2, rowIDX)
End If
cDGV.CurrentCell = CurrentCell
End If
ElseIf e.KeyCode = Keys.Delete Then
cDGV.CurrentCell.Value = ""
End If
Catch ex As Exception
EH.strRetVal = gfrmID & "/cDGV_KeyUp() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.strRetVal)
End Sub