title: Find the index of a string in a datagridview row?
body: <p>I want to find the index of a string in a <code>DataGridView</code> row and here is my code:</p>
  
  <pre><code> DataGridViewRow row = dgvVisual.Rows.Cast&lt;DataGridViewRow&gt;().FirstOrDefault(r =&gt; (string)r.Cells[0].Value == "9");
  </code></pre>
  
  <p>but the return value of <code>row</code> is <code>null</code> instead of a <code>DataGridViewRow</code>, how to solve this problem?</p>
  
  <p>The Whole code is here:</p>
  
  <pre><code>   SqlCommand cmd;
     cmd = new SqlCommand("select * from MyTable , SqlConnection);
       SqlDataReader read;
              read = cmd.ExecuteReader();
  
              while (read.Read())
              {
                  int ColIndex= int.Parse(read["ColumnName"].ToString()) + 1;
                  int RowIndex = -1;
                  DataGridViewRow row = dgvVisual.Rows.Cast&lt;DataGridViewRow&gt;().FirstOrDefault(r =&gt; (string)r.Cells[0].Value == (string)read["ColumnNameB"].ToString().Trim());
                  if (row != null)
                      RowIndex = row.Index;
                  DataGridViewCheckBoxCell chkbxCell = (DataGridViewCheckBoxCell)dgvVisual[ColIndex, RowIndex];
                  chkbxCell.Value = true;
  
              }
              read.Close();
  </code></pre>
  
  <p>Problem solved as there was some problem in the database and the read back was not correct.</p>