BlakeMcKenna
asked on
Which Events to use?
I have a DataGridView (dgv) on a Windows form. This dgv consists of several rows with 5 columns. The problem I'm having is not knowing which Events to fire.
The scenario is that if the cursor is in the 2nd column and the cell is empty and the User presses the Down/Up Arrow Keys, the program needs to display a message that the cell cannot be empty. I'm not sure what kind of logic to use to correctly handle this situation. Each cell only takes numeric data.
Thanks!
The scenario is that if the cursor is in the 2nd column and the cell is empty and the User presses the Down/Up Arrow Keys, the program needs to display a message that the cell cannot be empty. I'm not sure what kind of logic to use to correctly handle this situation. Each cell only takes numeric data.
Thanks!
You could use the DataGridView.CellContentCl ick Event to determine which cell in which row was clicked and use the DataGridView.CellLeave Event to make sure the cell is not empty before leaving.
ASKER
Saige,
So far I think the CellValidating will work, however, I still need some tweeks. Here is what my CellValidating Event looks like:
But here is my problem, when this event fires, the cl.Value has a string value of "" even though there is a numeric value in the cell so consequently it will execute the last If statement in the Event. I thought when the CellValidating Event fires...if there is data in a specific cell, it will show (I'm just declaring a variable as DataGridViewCell and assigning the CurrentCell to it).
So far I think the CellValidating will work, however, I still need some tweeks. Here is what my CellValidating Event looks like:
Private Sub dgvTension_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dgvTension.CellValidating
Try
Dim cl As DataGridViewCell = dgvTension.CurrentCell
EH.ErrorMessage = String.Empty
Select Case e.RowIndex
Case 0
Select Case e.ColumnIndex
Case 2 'Output Cell (0 Load)
If cl.Value = String.Empty Then
gblnEmptyCell = True
End If
End Select
Case Else
Select Case e.ColumnIndex
Case 1 'Output Cell
If cl.Value = String.Empty Then
gblnEmptyCell = True
End If
Case 2
End Select
End Select
If gblnEmptyCell Then
e.Cancel = True
gblnEmptyCell = False
EH.ErrorMessage = "Cell cannot be empty!" & "~I"
'dgvTension.CurrentCell = dgvTension(iTensionColIDX, iTensionRowIDX)
End If
Catch ex As Exception
EH.ErrorMessage = "frmCalibration_3/dgvTension_CellValidating() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
But here is my problem, when this event fires, the cl.Value has a string value of "" even though there is a numeric value in the cell so consequently it will execute the last If statement in the Event. I thought when the CellValidating Event fires...if there is data in a specific cell, it will show (I'm just declaring a variable as DataGridViewCell and assigning the CurrentCell to it).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
-saige-