title: DataGridView - Shortcut Keys Vs MouseClick for Cell Focus
body: <p>I've a weird situation while keyboard shortcuts of a button along with datagridview is used. Here is the explanation:</p>
  
  <p>I've a winform in which there is a datagridview and a save button. Save button has keyboard shortcut Alt+S. I enter some rows in the grid and I validate all the cells on Save button click and if cell value is invalid then I'll set the focus to that cell. Here is the code used to achieve that:</p>
  
  <pre><code>  for (var index = 0; index &lt; GridView.Rows.Count; index++)
    {
      var itemRow = GridView.Rows[index];
  
      if (itemRow.Cells[SomeColumn.Name].Value.ToString() == string.Empty)
      {
         GridView.CurrentCell = itemRow.Cells[SomeColumn.Name];
         GridView.Focus();
      }
    }
  </code></pre>
  
  <p>Above code works fine and sets the focus in the particular cell with cursor when I click on the save button.</p>
  
  <p>But, when I use shortcut key, focus is not set to the cell at all. I'm not sure why it is behaving like this.</p>
  
  <p>Out of curiosity I've tested an event of the datagridview. CellLeave event of datagridview is fired when I click on save button, but when I use shortcut for save button it doesn't get fired.</p>
  
  <p>Why is it behaving like this? any inputs? How can I set the focus when I use shortcuts?</p>
  
  <p><strong>Update</strong>: I've done a trick to work it properly. I've added SaveButton.Focus() code in the button click event of the save button as first line. This is redundant but forcing the grid to lose the focus when user uses short cut key to access this button. This solution is working fine, but not sure what could be the reason for the above situation. Still looking for an answer. :(</p>