Fendy Plick Fendy Plick - 2 years ago 163
Vb.net Question

keypress on datagridview only character "abcde" and convert to uppercase

How to limit

only character abcde and convert to uppercase at vb.net?

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 3 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBoxabcde_keyPress
End If
End Sub

Private Sub TextBoxabcde_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And e.KeyChar <> "." Then
e.Handled = True
End If
End Sub

Answer Source


Private Sub DataGridView1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
    Dim allLetters As String = "abcde"
    If Not allLetters.Contains(e.KeyChar.ToString.ToLower) Then
        e.KeyChar = ChrW(0)
        e.Handled = True
    End If
End Sub

Private Sub dataGridView1_CellFormatting(sender As Object, e As 
    If e.Value IsNot Nothing Then
        e.Value = e.Value.ToString().ToUpper()
        e.FormattingApplied = True
    End If
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download