Search code examples
vb.netdatagridviewcellisnumeric

Datagridview cell value evaluation vb.net


I am trying to process my datagridview cell data (of a specific column) to evaluate the user input firstly for a numerical value then for values ranging betweened 0 and 90... This code works almost the same for a textbox situation but when i try and apply it to a datagridview cell situation everything goes miss.. how can i fix this?

    Private Sub DataGridView1_EditingControlShowing1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        Dim txtedit As TextBox = DirectCast(e.Control, TextBox)
        AddHandler txtedit.KeyPress, AddressOf txtEdit_KeyPress
    End If
End Sub

    Private Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    If IsNumeric(DataGridView1.CurrentCell.Value) Then 
        'VALIDATES CURRENT CELL INPUT TO VALUES RANGING BETWEEN 1 AND 90°...
        If DataGridView1.CurrentCell.Value <= 0 Or DataGridView1.CurrentCell.Value >= 91 Then
            MessageBox.Show("The angle you are trying to achieve is " & DataGridView1.CurrentCell.Value & "°." & vbCrLf & vbCrLf & "Only values ranging from 1° to 90° are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Else
            'NOTHING
        End If
    Else
        MessageBox.Show("Only numeric values are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    End If
End Sub

Solution

  • You can handel DataGridView1_CellEndEdit:

       Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    
            Try
                Dim order As DataGridView = DirectCast(sender, DataGridView)
    
                If IsNumeric(order("NmeOfColumn", e.RowIndex).Value) Then
    
                    If order("NmeOfColumn", e.RowIndex).Value <= 0 Or order("NmeOfColumn", e.RowIndex).Value >= 91 Then
                        MessageBox.Show("The angle you are trying to achieve is " & DataGridView1.CurrentCell.Value & "°." & vbCrLf & vbCrLf & "Only values ranging from 1° to 90° are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                    Else
                        'NOTHING
                    End If
                Else
                    MessageBox.Show("Only numeric values are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End If
    
    
            Catch ex As Exception
    
            End Try
        End Sub