Menu Horisontal

Senin, 23 April 2012

DataGridView Input Validation (VB.NET)


Dari : http://www.daniweb.com/software-development/vbnet/threads/334424/datagridview-input-validation-vb.net

Can someone tell me / post some codes here on how could I validate mydatagridview? 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:

This worked for me without any error:
  1. Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
  2. If (e.ColumnIndex = 1) Then ' Checking numeric value for Column1 only
  3. If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value <> Nothing Then
  4. Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
  5. If Not Information.IsNumeric(value) Then
  6. MessageBox.Show("Please enter numeric value.")
  7. DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
  8. Exit Sub
  9. End If
  10. End If
  11. End If
  12. End Sub
  13. Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
  14. MessageBox.Show("Please enter a numeric value")
  15. End Sub

Tidak ada komentar: