i have below code for my custom textbox to allow numeric chatecters for entering amount.

Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)

 If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or _
Asc(e.KeyChar) = 46 Or _
Asc(e.KeyChar) = 13 Or _
Asc(e.KeyChar) = 8 Then
If Asc(e.KeyChar) = 46 Then
If InStr(1, Me.Text, ".") > 0 Then
e.Handled = True
End If
End If
Else
e.Handled = True
End If
End Sub

Same numeric i want for my custom Datagridview class for column index 3 . its not working

 Public Class DataGridPosting
Inherits DataGridView
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)



 If Me.CurrentCell.ColumnIndex = 3 Then
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or _
Asc(e.KeyChar) = 46 Or _
Asc(e.KeyChar) = 13 Or _
Asc(e.KeyChar) = 8 Then

Else
e.Handled = True
End If
End If
End Sub

End Class

How can i make this work

Hi Tinstaafi

I have this code which works correctely but i want to put this in my custom Datagridview .I have to add this datagridview at on 12 to 15 forms and want to avoide writing code each and every form.

How can i put this on custom control.

Private Sub DgvInvDet_EditingControlShowing1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DgvInvDet.EditingControlShowing

' below code on form
If DgvInvDet.CurrentCell.ColumnIndex = 3 Then
'Only Keys for amount
RemoveHandler e.Control.KeyPress, AddressOf MsubValidateKeyPress
RemoveHandler e.Control.KeyPress, AddressOf MsubValidateKeyPress
AddHandler e.Control.KeyPress, AddressOf MsubValidateKeyPress
Else
RemoveHandler e.Control.KeyPress, AddressOf MsubValidateKeyPress
RemoveHandler e.Control.KeyPress, AddressOf MsubValidateKeyPress
End If
End Sub

'below code on module
Public Sub MsubValidateKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 46 Then '
Else
e.Handled = True
End If
End Sub

The main trouble is the datagridview keyPress event doesn't fire from inside the cell, and the cell doesn't have a keypress event. You're probably stuck with validating the cell upon leaving and return the focus if the input is invalid.

You could google "keydown event in datagridviewtextboxcell" and find lots of articles on the subject.

Actually i want to create custom control for Datageidview which will valadate keypress for numeric values in coloumn 3 and for that i want to put above code in mycustom datagridview .

This question has already been answered. Start a new discussion instead.