# CodeGuru Technical FAQs > CodeGuru Individual Visual Basic FAQs >  VB.NET 2005 DataGridView : How to do DataGridView cell validation?

## aniskhan

*Q:* How to do DataGridView cell validation?

*A:* If you want take input directly in datagridview, column of string type takes any input but DataGridView default error dialog is shown when making invalid input i-e 123a for integer type column that asks to handle the DataError event of datagridview .
So you can use CellValidating event of DataGridView to validate integer type column as well as other.

*Steps:*Use DataGridView's CellValidating eventGet the cell for which the event is calledIf the cell is in EditMode thenPerform validation
CellValidating Event:


```
    Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)

        If cell.IsInEditMode Then
            Dim c As Control = DataGridView1.EditingControl

            Select Case DataGridView1.Columns(e.ColumnIndex).Name
                Case "sessno", "rno"
                    c.Text = CleanInputNumber(c.Text)
                Case "name"
                    c.Text = CleanInputAlphabet(c.Text)
            End Select
        End If
    End Sub
```

Utility Functions:

```
    Private Function CleanInputAlphabet(ByVal str As String) As String
        Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "")
    End Function
```



```
    Private Function CleanInputNumber(ByVal str As String) As String
        Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-.]", "")
    End Function
```

----------

