Code for Datagridview cell allows only integers

Code for Datagridview cell allows only integers


To use this feature we want to create datagridview dynamic cell by using the following code.

dim col as new datagridviewtextboxcolumn

Private Sub datagridview1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv_view.EditingControlShowing
'---restrict inputs
Dim tb As TextBox
tb = CType(e.Control, TextBox)
If datagridview1.CurrentCell.ColumnIndex = 2 Then
'---add an event handler
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress

RemoveHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If
End Sub

'then take textbox keypress event

Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.KeyChar = Allowchars(e.KeyChar)
End Sub

Note:Write your own function Allowchars()for allowing only characters or inplace you use any function like allow integers only or etc.



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: