title: c# check if datagridview column contains value & linq First() vs FirstOrDefault()
body: <p>I need to check if first column in <code>DataGridView</code> contains value 10</p>
  
  <p>I used this</p>
  
  <pre><code>int index = -1;  
  index = (dgv.Rows.Cast&lt;DataGridViewRow&gt;()  
          .Where(r =&gt; r.Cells[0].Value.Equals(10))  
          .Select(r =&gt; r.Index)).First();  
  </code></pre>
  
  <p>but sometimes produce error: Sequence contains no elements</p>
  
  <p>Someone on this forum sugested to use <code>FirstOrDefault()</code> no <code>First()</code></p>
  
  <p>Thats work BUT if first column in first row contains value 10 
  or there are no rows where first column contains value 10, variable index have value 0</p>
  
  <p>My solution was:</p>
  
  <pre><code>try {
   index = (dgv.Rows.Cast&lt;DataGridViewRow&gt; ()
    .Where(r =&gt; r.Cells[0].Value.Equals(10))
    .Select(r =&gt; r.Index)).First();
  } catch {
   index = -1
  }
  </code></pre>
  
  <p>Best regards,</p>