Ankit Agarwal

Ankit Agarwal

  • NA
  • 379
  • 248.1k

How to clear datagridview typed cell value in c# .net window

Sep 5 2013 6:30 AM
Hello,

How to clear datagridview typed cell value in c# .net windows application?

I have a datagridview in my application i am validating our datagridview for numneric value,
I am using this code:-

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            
                DataGridViewTextBoxCell cell = dataGridView1[6, e.RowIndex] as DataGridViewTextBoxCell;
                if (cell != null)
                {
                    if (e.ColumnIndex == 6)
                    {
                        char[] chars = e.FormattedValue.ToString().ToCharArray();
                        foreach (char c in chars)
                        {
                            if (char.IsDigit(c) == false)
                            {
                                MessageBox.Show("You have to enter digits only");
                                e.Cancel = true;
                                break;
                            }
                        }
                    }
                }
            }

        }

But i have a problem with this, when we typed in cell so it did not clear cell after the message show,
I have to press, Backspace key for clear the cell.

I want to automatically cleared after wrong entry or "MessageBox.Show("You have to enter digits only");" this message show.

How it can be possible?

Please help me.

Thanks in Advance
Ankit Agarwal
Software Engineer 

Answers (2)