Windows Develop Bookmark and Share   
 index > Windows Forms General > Prevent alphabets, special chaacters to be entered in the column for phone number.
 

Prevent alphabets, special chaacters to be entered in the column for phone number.

Hi all,

Using string.format, i formatted the phone number column in a datagridview to display in 123-456-7890
Now, I want to make sure that only numerals are entered into that column.

How can i do this?

Thanks in Advance

rowter  Monday, September 14, 2009 3:10 PM
Hi,
using cellvalidating event youcan validate the cell.

i hope it will help you

      private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "";
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (cell.IsInEditMode)
            {
                if (e.FormattedValue == null || e.FormattedValue.ToString() == string.Empty)
                {
                    return;
                }
                else
                {
                    string name = (string)e.FormattedValue;
                    Match match = Regex.Match(name, @"^[0-9\s]*$");
                    if (match.Value == string.Empty)
                    {
                        e.Cancel = true;
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Please Enter The Valid Phone No";

                    }
                    if (match.Value.Length > 25)
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Please Enter The Valid Phone No";
                        e.Cancel = true;
                    }
                }
            }

    }


Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Monday, September 14, 2009 3:20 PM
Hi,
using cellvalidating event youcan validate the cell.

i hope it will help you

      private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "";
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (cell.IsInEditMode)
            {
                if (e.FormattedValue == null || e.FormattedValue.ToString() == string.Empty)
                {
                    return;
                }
                else
                {
                    string name = (string)e.FormattedValue;
                    Match match = Regex.Match(name, @"^[0-9\s]*$");
                    if (match.Value == string.Empty)
                    {
                        e.Cancel = true;
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Please Enter The Valid Phone No";

                    }
                    if (match.Value.Length > 25)
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Please Enter The Valid Phone No";
                        e.Cancel = true;
                    }
                }
            }

    }


Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Monday, September 14, 2009 3:20 PM
Thanks Gnanadurai.

I did nt get a chance to work on it. I will update you as soon as i work on it.

rowter  Tuesday, September 15, 2009 2:15 PM

You can use google to search for other answers

Custom Search

More Threads

• creating a form to edit elements of an object.
• Is there any property to keep a form on top but within an application?
• ComboBox Search AutoSuggest
• need to dock mainmenu with menustrip
• Read excel to dataset
• Invalid Icon in task bar for thread-owned MsgBox
• loop through 15 check boxes in a panel
• Hiding Form Updates
• Control box close button function
• Update UI of winform-1 with winform-2 ,with backgroundprocess