Validation in a single column DataGridView

How do I check on a specific DataGridViewTextBoxColumn column in my DataGridView so that the user can enter a value in it?

+5
source share
1 answer

I think you are looking for the correct validation of a column of a text field in a datagrid format? if so you could take a look at this link

http://www.codeproject.com/Questions/93691/Validations-inside-DataGridView-TextboxColumn.aspx

EDIT 1:

You can use this solution, but it only checks numbers or if you want to check the text, you can change the code.

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    DataGridViewTextBoxCell cell = dataGridView1[2, e.RowIndex] as DataGridViewTextBoxCell;

    if (cell != null)
    {
         if (e.ColumnIndex == 2)
         {
             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;
                    }
              }
          }
     }
}

NOTE: this code is not verified.

+6
source

All Articles