Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

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!
Avatar of it_saige
it_saige
Flag of United States of America image

Use the CellValidating event.  Example code from MSDN:
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = "" 
    Dim newInteger As Integer 

    ' Don't try to validate the 'new row' until finished  
    ' editing since there 
    ' is not any point in validating its initial value. 
    If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return 
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True 
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer" 

    End If 
End Sub

Open in new window

-saige-
You could use the DataGridView.CellContentClick 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.
Avatar of BlakeMcKenna

ASKER

Saige,

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

Open in new window


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
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial