none
Data validating RRS feed

  • Question

  •  

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

    If e.ColumnIndex <> 10 OrElse e.FormattedValue.ToString = "" Then Exit Sub

    Dim Value As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value)

    If Value < 8 Or Value > 40 Then

    MsgBox("Please input a value between 8 and 40")

    e.Cancel = True

    End If

    If e.ColumnIndex <> 11 OrElse e.FormattedValue.ToString = "" Then Exit Sub

    Dim Value2 As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value2)

    If Value2 < 0 Or Value2 > 40 Then

    MsgBox("Please input a value not lower than 0 and not greater than 40")

    e.Cancel = True

    End If

    If e.ColumnIndex <> 14 OrElse e.FormattedValue.ToString = "" Then Exit Sub

    Dim Value3 As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value3)

    If Value3 < 0 Then

    MsgBox("Please input a value greater than 0")

    e.Cancel = True

    End If

    End Sub

     

    Does anyone have any idea what's wrong with this code.  The first columnIndex <>10 part, work exactly just fine, however, when i create multiple times.  The rest it doesn't work.  I have been trying to figure it out, but i have no clue about it.  The purpose for this code is to check the cell column whether it has the correct value, if not print the message.

     

    Please and thank you

    Friday, March 7, 2008 4:52 PM

Answers

  • Hi,

     

    I don't understand your logic. Assume the String isn't empty, only column with index 10 will pass the first test. I guess you want to something like

    If e.FormattedValue.ToString = "" Then Exit Sub

    Select Case e.ColumnIndex

    Case 10

    Dim Value As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value)

    If Value < 8 Or Value > 40 Then

    MsgBox("Please input a value between 8 and 40")

    e.Cancel = True

    End If

    Case 11

    Dim Value2 As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value2)

    If Value2 < 0 Or Value2 > 40 Then

    MsgBox("Please input a value not lower than 0 and not greater than 40")

    e.Cancel = True

    End If

    Case 14

    Dim Value3 As Integer

    Integer.TryParse(e.FormattedValue.ToString, Value3)

    If Value3 < 0 Then

    MsgBox("Please input a value greater than 0")

    e.Cancel = True

    End If

    End Select

     

    regards

    Philipp

    Friday, March 7, 2008 7:36 PM