Dear Sir,

How to combine these two conditions in one section

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating

'condition 1       

        ' check given value is numeric or charactr type
        If e.ColumnIndex = 3 Then
            If Not IsNumeric(e.FormattedValue) Then
                DataGridView1.Rows(e.RowIndex).ErrorText = " must be a numeric value"
                MsgBox("Must be a numeric value")
                DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = Nothing
                e.Cancel = True
            End If
        End If

'condition 2
        ' check given value is greater than 0 or not 
        If e.ColumnIndex = 3 Then
            If (e.FormattedValue.ToString = "0") Then
                e.Cancel = True
                MsgBox(" must be greater than 0")
            End If
        End If
    End Sub

hi i am a beginner but will still try to slove this. i have posted the solution below but not tested yet. I hope it helps

If e.ColumnIndex = 3 Then
            If Not IsNumeric(e.FormattedValue) And (e.FormattedValue.ToString = "0") Then
                DataGridView1.Rows(e.RowIndex).ErrorText = " must be a numeric value"
                MsgBox("Must be a numeric value")
                DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = Nothing
                e.Cancel = True
                MsgBox(" must be greater than 0")

            End If
        End If

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

another way is to use if-else structure:

If e.ColumnIndex = 3 Then 
        If Not IsNumeric(e.FormattedValue) Then
               DataGridView1.Rows(e.RowIndex).ErrorText = " must be a numeric value"
                MsgBox("Must be a numeric value")
                DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = Nothing
                e.Cancel = True
        ElseIf (e.FormattedValue.ToString = "0") Then
                e.Cancel = True
                MsgBox(" must be greater than 0")            
        End If
End If

Some situations dont lend themselves to a combined condition, an if-else structure will test each condition from teh top down until it meets one so be careful abou thte order of your conditions

Comments
Got there before I could answer! Good solution
This article has been dead for over six months. Start a new discussion instead.