title: C# Winforms datagridview cell validation
body: <p>I have a datagridview with 3 columns: Guid(invisible), checkbox column and string column.</p>
  
  <p>I need to validate what user entered into string column.
  Possible cases:</p>
  
  <p>1) Dont allow to enter duplicate record</p>
  
  <p>2) Dont allow to enter empty record</p>
  
  <p>Need to keep in mind: new row or editing row. For example if empty string for new row and user leaves it - i must just cancel this new row.</p>
  
  <p>But the problem is with checkbox column. When i check that checkbox and leave it (text column is empty) - i unable to validate that. So this is the bottleneck of my code</p>
  
  <pre><code> private void dgvKeywords_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
      {
          if (e.RowIndex &lt; 0 || e.ColumnIndex != 2)
              return;
  
          string keyWord = e.FormattedValue == null ? string.Empty : e.FormattedValue.ToString();
          if (string.IsNullOrWhiteSpace(keyWord))
              if (!dgvKeywords.Rows[e.RowIndex].IsNewRow)
              {
                  e.Cancel = true;
                  MessageBox.Show("Keyword cannot be empty", "Keyword processing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  return;
              }
  
          var list = bsCustomerLanguageKeywords.DataSource as List&lt;CustomerLanguageKeyword&gt;;
          if (list == null)
              throw new Exception("Unexpected type in the keywords grid");
  
          var separatedList = list.Where((t, i) =&gt; i != bsCustomerLanguageKeywords.Position).ToList();
  
          bool nonUnique = separatedList.Count(x =&gt; x.Word != null &amp;&amp; x.Word.ToLower() == keyWord.ToLower()) &gt; 0;
          if (nonUnique)
          {
              e.Cancel = true;
              MessageBox.Show("Keyword must be unique", "Keyword processing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              return;
          }
      }
  </code></pre>