Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am facing a problem in my email address validation codes. when i click on that particular cell (email Address Column),
whether or not i want to enter in that cell it does not allow me to move to the next cell..... i am pasting my codes here.!! Please help sooon..!!!
Thanks in Advance
Private Sub DataGridView2_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgCompany.CellValidating
        If e.ColumnIndex = 6 Then
            If Not IsEmailValid(e.FormattedValue) Then
                MsgBox("Enter Valid Email Address")
                e.Cancel = True
                Dim result As DialogResult = MessageBox.Show("The Email Address is not valid." & " Do you want re-enter it?", "Invalid Email Address", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
                If result = Windows.Forms.DialogResult.Yes Then
                    e.Cancel = True
                End If
            End If
        ElseIf dgCompany.CurrentRow.Cells(6).Value.ToString() = String.Empty Then
            e.Cancel = False
        End If
    End Sub
    Function IsEmailValid(ByVal EmailAddress As String) As [Boolean]
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]" & "*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim emailAddressMatch As Match = Regex.Match(EmailAddress, pattern)
        If emailAddressMatch.Success Then
            IsEmailValid = True
        Else
            IsEmailValid = False
        End If
    End Function
Posted
Updated 15-Jan-12 20:21pm
v2

1 solution

If you want take input directly in datagridview, column of string type takes any input but DataGridView default error dialog is shown when making invalid input i-e 123a for integer type column that asks to handle the DataError event of datagridview .
So you can use CellValidating event of DataGridView to validate integer type column as well as other.

Steps:
Use DataGridView's CellValidating event
Get the cell for which the event is called
If the cell is in EditMode then
Perform validation

CellValidating Event:
VB
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)

If cell.IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl

Select Case DataGridView1.Columns(e.ColumnIndex).Name
Case "sessno", "rno"
c.Text = CleanInputNumber(c.Text)
Case "name"
c.Text = CleanInputAlphabet(c.Text)
End Select
End If
End Sub

VB
Utility Functions: Private Function CleanInputAlphabet(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "")
End Function 

Private Function CleanInputNumber(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-.]", "")
End Function
 
Share this answer
 
Comments
Cluelesssssss 18-Jan-12 0:37am    
Thank you ...!!! I shall Try it out..!! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900