i am doing a project on window form in c#
i have a DataGridView with data from the database
now i want to do editing in data GridView, and i have this column call Name, but and i want to validate so that when user edit, it will only key in alphabet, like example, in the Name column row 1 has a name call Rosy Chin, then user edit to this Rosy Ch11n …it should prompt the user say that only alphabet, i use the code below but it don’t prompt me that message…however if the user edit to this 4Rosy Chin….it will appear the prompt message…can i know where am i wrong??
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
//int indicator;
int newInteger;
if (e.ColumnIndex == 1)
{
if (dataGridView1.Rows[e.RowIndex].IsNewRow) return;
String data = e.FormattedValue.ToString();
String validate= @"^[a-zA-Z]";
Regex nameAlphabet = new Regex(validate);
String nameGridView = data;
if(!(nameAlphabet.IsMatch(nameGridView )))
{
e.Cancel = true;
MessageBox.Show(@"Name must be in Alphabet!");
}
else
return;
}
}
Try to fix your RegEx:
Description:
Good RegEx sandbox.
To not allow only s space:
Link to the sandbox.