title: Validations for Datagridview cell values in C#
body: <p>I have a windows form application which contains a datgridview. I need to enforce cell validations on the datagridview cells so that it does not accept negative values. I found a suitable code for it from the msdn library.</p>
  
  <pre><code>private void dataGridView1_CellValidating(object sender,
  DataGridViewCellValidatingEventArgs e)
  {
  dataGridView1.Rows[e.RowIndex].ErrorText = "";
  int newInteger;
  
  // Don't try to validate the 'new row' until finished  
  // editing since there 
  // is not any point in validating its initial value. 
  if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
  if (!int.TryParse(e.FormattedValue.ToString(),
      out newInteger) || newInteger &lt; 0)
  {
      e.Cancel = true;
      dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a Positive integer";
  }
  }
  </code></pre>
  
  <p>Unfortunately the code only allows integers to be entered in the datagridview. Since i have a column name as "Item Name" which is supposed to be entered as text there is an issue in the code. It generates an error message when i type in the name of the item.The rest of the cell validations are perfectly working! How am i supposed to edit the code so that it does not generate this error?</p>
  
  <p>Thanks in advance</p>
  
  <p>Mirfath</p>