Validate Email Id in Datagridview cell
How to validate datagridview cell for valid email id. in C#.net winform
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shankar MPosted Apr 26, 2013, 11:58 PM
You can validate the E-mail Id in Datagridview's CellValidatingEvent Something like this,
Presume that dgv is the name of the DataGridview and MailId is one of the column of Datagridview.
string HeaderText = dgv.Columns[e.ColumnIndex].HeaderText;
This is to get the header Text of the Column, to check that we are in the E-mail id column.
if (!HeaderText.Equals("MailId")) return;
Abort validation if the cell is not MailId column.
if(string.IsNullOrEmpty(e.FormattedValue.ToString()))
{
MessageBox.Show("Please Enter the mail Id");
e.Cancel = true; // Will not allow us to Leave the cell until an non-empty string is entered.
}
You can also use Regular Expressions to validate that the E-Mail Id entered is Valid.