title: Setting focus to a DataGridView row
body: <p>The following source code is intended to set focus to the immediately previous row of a deleted row.</p>
  
  <p><a href="https://i.stack.imgur.com/U5LZW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U5LZW.png" alt="enter image description here"></a></p>
  
  <p>Suppose I want to delete an unwanted word <code>dddddddd</code> from the database. When I press the <code>Delete</code> button, I want the word <code>cynosure</code> to be focused and placed at the top of the <code>DataGridView</code> which is not the case right now. </p>
  
  <p><a href="https://i.stack.imgur.com/O7Yc2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O7Yc2.png" alt="enter image description here"></a></p>
  
  <p>Right now, it is displayed at the bottom.</p>
  
  <p><a href="https://i.stack.imgur.com/VJG4F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VJG4F.png" alt="enter image description here"></a></p>
  
  <p><strong>Source Code</strong></p>
  
  <pre><code>    void SetFocusToWord(Word concernedWord)
      {
          if (concernedWord != null)
          {
              int index = 0;
              foreach (DataGridViewRow r in dataGridView1.Rows)
              {
                  Word item = r.Tag as Word;
  
                  if (concernedWord.Name == item.Name)
                  {
                      dataGridView1.Focus();
                      dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
  
                      break;
                  }
  
                  index++;
              }
          }
      } 
  
  private void btnDelete_Click(object sender, EventArgs e)
          {
              try
              {
                  if (dataGridView1.SelectedRows.Count &gt; 0)
                  {
                      int selectionIndex = dataGridView1.SelectedRows[0].Index;
  
                      foreach (DataGridViewRow r in dataGridView1.SelectedRows)
                      {
                          Word c = r.Tag as Word;
  
                          if (c != null)
                          {
                              _wordDatabase.Delete(c);
                          }
                      }
  
                      LoadToDataGridView();
  
                      if(selectionIndex &gt; 0)
                      {
                          selectionIndex = selectionIndex - 1;
                      }
  
                      Word item = dataGridView1.Rows[selectionIndex].Tag as Word;
  
                      SetFocusToWord(item);
                  }
                  else
                  {
                      throw new Exception(SelectionErrorMessages.GetErrorMessageFor(typeof(Word)));
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          } 
  
      void LoadToDataGridView()
      {
          dataGridView1.Rows.Clear();
  
          List&lt;Word&gt; items = (List&lt;Word&gt;)_wordDatabase.Get();
  
          if (items != null)
          {
              if (items.Count &gt; 0)
              {
                  int i = 0;
                  foreach (Word c in items)
                  {
                      dataGridView1.Rows.Add(c.Name, c.Hint);
                      dataGridView1.Rows[i].Tag = c;
                      ++i;
                  }
              }
          }
      } 
  </code></pre>