Hi guys,
Could some one please help me in validating a datagrid coloumn..,i want only alphanumeric values.,symbols like -,+..etc were not allowed..folliwing is the code wht i have.,but it allows only alphabets but not numerals..

If Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
            If Asc(e.KeyChar) = 46 Then
                If CType(sender, TextBox).Text.Contains(Chr(46)) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            Else
                e.Handled = False
            End If
        Else
            e.Handled = True
        End If

Try This...

Private Sub Datagridview1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Datagridview1.EditingControlShowing
        Try
            If (Datagridview1.CurrentCell.ColumnIndex = 2) Then
                set_flg = True
                Dim textBoxCell As TextBox = CType(e.Control, TextBox)
                AddHandler textBoxCell.KeyPress, AddressOf Me.textBoxCell_keyPress
                
            Else
                set_flg = False
            End If
        Catch ex As Exception

        End Try
        
    End Sub

    Public Sub textBoxCell_keyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            If (set_flg = True) Then
                       If (Microsoft.VisualBasic.Asc(e.KeyChar) >= 65 And Microsoft.VisualBasic.Asc(e.KeyChar) <= 90) Or (Microsoft.VisualBasic.Asc(e.KeyChar) >= 97 And Microsoft.VisualBasic.Asc(e.KeyChar) <= 122) Or Microsoft.VisualBasic.Asc(e.KeyChar) = 46 Or Microsoft.VisualBasic.Asc(e.KeyChar) = 32 Or Microsoft.VisualBasic.Asc(e.KeyChar) = 8 Or Microsoft.VisualBasic.Asc(e.KeyChar) = 13 Then

                    e.Handled = False
                    lblmsg.Visible = False
                Else
                    lblmsg.Visible = True
                    lblmsg.Text = "Enter Character only"
                    e.Handled = True
                End If
            End If
        Catch ex As Exception

        End Try
        
    End Sub

Cheers....

Hi Bhagvat,
thnaks for ur reply.im gettig a compile error tht set_flg is not declared..
how do i solve th

Declare

Dim set_flg As Boolean = False

at the top of the class

Hi Bhagvat,
Iam able to enter only alphabets..
my requirement is to enter both alphabets and numbers but not symbols..

Just find the ASCII values for symbols which you don't want here

Thts fantastic Bhagvat,i got what i want...thanks alot..god bless

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.