title: how to select datagridview row programmatically in c#
body: <p>I need to select an entire row if a cell in column 0 contains specified value. I have a TextBox and DaraGridView.when I put a value exist in gridview row selected without problem  But when a put doesn't exist in gridview the program throws an exception
  thank you in advance!!</p>
  
  <pre><code>private void Searchvalue_TextChanged(object sender, EventArgs e)
  {
      dataGridView1.ClearSelection();
      var enteredText = (sender as TextBox).Text;
      int rowIndex = -1;
  
      bool tempAllowUserToAddRows = dataGridView1.AllowUserToAddRows;
  
      // Turn off or .Value below will throw null exception
      dataGridView1.AllowUserToAddRows = false; 
      if (enteredText != String.Empty)
      {
          DataGridViewRow row = dataGridView1.Rows
              .Cast&lt;DataGridViewRow&gt;()
              .Where(r =&gt; r.Cells["Column1"].Value.ToString().Contains(enteredText)).First();
          rowIndex = row.Index;
          dataGridView1.AllowUserToAddRows = tempAllowUserToAddRows;
          dataGridView1.Rows[rowIndex].Selected = true;
          dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[rowIndex].Index;
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/MCLWf.png" alt="DataGridView Image"></p>