Hi,
I have a datagridview on my form.There is a column of roll no in the datagridview.
I want that whenever user presses anyother key than a number key(ex:1,2,3,4....)than immediately i should get a messagebox that only numbers allowed.
I have tried the below code on datagridView Cellvalidation event but its not working.
Please suggest on what event and how can i do such validation.

If GridView2.Item(2, GridView2.CurrentCell.RowIndex).Selected = True Then
            If e.ColumnIndex = 3 Then
                If Not IsNumeric(e.FormattedValue) Then
                    GridView2.Rows(e.RowIndex).ErrorText = _
                       "Please Insert only numbers."
                    e.Cancel = True
                End If
            End If
        End If

Ur letting the user to enter the data in gridview cell?

Yes im letting the user to insert roll No's

Try your code in Cell Enter event, cell leave event and see. And if Not valid entry then dont forget to clear the cell.

m getting error on e.FormattedValue as FormattedValue is not a member of System.Windows.Forms.DataGridViewCellEventArgs

Why ur doing E.formatedvalue? instead you can get the cell text using Activecell.text right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.