Can someone tell me / post some codes here on how could I validate my datagridview? I mean, a certain column on my datagridview should accept integers only, otherwise, it will return a messagebox. Kindly include on which event it should be posted. Thank you very much. God Bless. :D

Hi!

Check this:

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If (e.ColumnIndex = 0) Then   ' Checking numeric value for Column1 only
            Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            For Each c As Char In value
                If Not Char.IsDigit(c) Then
                    MessageBox.Show("Please enter numeric value.")
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                    Exit Sub
                End If
            Next
        End If
    End Sub

Another Alternative using same event:

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If (e.ColumnIndex = 0) Then   ' Checking numeric value for Column1 only
            Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            If Not Information.IsNumeric(value) Then
                MessageBox.Show("Please enter numeric value.")
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                Exit Sub
            End If
        End If
    End Sub

Edited 5 Years Ago by ShahanDev: n/a

It's great but there are some errors. I'm so sorry. The message box only appears when I leave the column blank.

Edited 5 Years Ago by paulablanca: n/a

Hmm... glad to see that your problem "Solved". :)
Please close thread, so that other expert may avoid to comment on it.

I'm so sorry but I'm having problems. The column I want to validate is column 4. since the codes is (e.ColumnIndex = 1) there are some errors when I change it to (e.ColumnIndex = 4). :(

You should write:
e.ColumnIndex = 3

since index is zero-based.

If this don't help please post the error message.

Actually, I hid the index 0 of my datagrid to prevent users from editing them. So, now my index is 1. when I changed e.columnindex = 3 it worked for my column 3(Middle Name) . But when I used for e.columnindex = 4 for my column 4 (Contact Number where I need it) it posted errors, there some exception reports.

Edited 5 Years Ago by paulablanca: n/a

Hi!

This line:

If (e.ColumnIndex = 1) Then

works for the Visible columns so no need to take care of the InVisible columns. Also in this line:

Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()

e.RowIndex and e.ColumnIndex will get value from the visible columns of the DataGridView so I don't think so that the error should raise if value provided correctly.

Thanks. I am thinking of that as well. There is no error regarding about the program, I guess. Errors like System.FormatException: Input string was not in correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&number, NumberFormatInfo info, Boolean parseDecimal) showed. What I am thinking now is that column 4 (Contact Number is declared as integer in mysql and the rest are varchar and it works on other columns except contact number).

Edited 5 Years Ago by paulablanca: n/a

I'm so sorry if I have a lot of questions. Is it possible if the input is like Dim value as integer? Like in here: Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() ---> I think the DB reads it as string instead of integer when contact number column is declared int in sql.

Edited 5 Years Ago by paulablanca: n/a

".Value" returns "object" datatype. so we can cast it into its appropriate type.
I found this error on PrimaryKey Column. Could you please check by adding this code:

Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        MessageBox.Show("Please enter a numeric value")
    End Sub

This worked for me without any error:

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If (e.ColumnIndex = 1) Then   ' Checking numeric value for Column1 only
            If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value <> Nothing Then
                Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
                If Not Information.IsNumeric(value) Then
                    MessageBox.Show("Please enter numeric value.")
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                    Exit Sub
                End If
            End If
        End If
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        MessageBox.Show("Please enter a numeric value")
    End Sub

Thank you very much! This worked on me flawlessly. I tried DataError before because that's what the error suggest me to do but still I didn't get it. I'm so sorry for disturbing you. But thank you very much, you helped me a lot. May God Bless you. :D

I am here to provide you help, so nothing disturbed me.
If you think that your problem solved, close the thread.

Again, thank you very much. I hope if I have to ask regarding programs again, you'd be helping me still. Continue helping others. God Bless us all. :D

Edited 5 Years Ago by paulablanca: n/a

The respected DaniWeb !
Thanks big!
It is very a pity, that I do not know English, I write by means of the translator.
Valeri

This article has been dead for over six months. Start a new discussion instead.