title: Cannot set CurrentCell focus to first editable column in DataGridView
body: <p>I am trying to select programatically the first editable column of a datagridview and cannot.</p>
  
  <p>Column 0 and 1 are hidden.  Column 2 is read-only showing line number.  Columns 3 and on are editable.</p>
  
  <p>If I do this:</p>
  
  <pre><code>grd.CurrentCell = grd.Rows[x].Cells[2];
  </code></pre>
  
  <p>The computer actually beeps and selects the first read-only column instead of selecting the first editable column.</p>
  
  <p>If I do this:</p>
  
  <pre><code>grd.CurrentCell = grd.Rows[x].Cells[3];
  </code></pre>
  
  <p>It selects (focus) the third visible column (The second editable column) correctly. I cannot select the second visible column (the first editable column). What am I doing wrong?</p>
  
  <p>To answer the question of when I am selecting the column, it is during the last tab event of the previous row.  I have a class to do this which inherits DataGridView.  Here is the relevant code in that class:</p>
  
  <p>...</p>
  
  <pre><code>protected override bool ProcessDialogKey(Keys keyData)
          {
  
              switch (keyData)
              {
                  case Keys.Tab:
                      // Check if the tab key was pressed in the last column/row
                      if (currentCol == this.ColumnCount - 1 &amp;&amp; currentRow == this.RowCount - 1)
                      {
                          LastTabReachedEventArgs ev = new LastTabReachedEventArgs();
                          ev.currentCol = currentCol;
                          ev.currentRow = currentRow;
                          OnLastTabReached(this, ev);
                          return false;
                      }
                      break;
                  case Keys.Enter:
                      // nothing to do
                      break;
              }
              return base.ProcessDialogKey(keyData);
  
          }
  </code></pre>
  
  <p>As you can see, the last column raises the OnLastTabReached which is handled in my form to select the first editable column of the next record.  Which fails:</p>
  
  <pre><code> private void grd_LastTabReachedEvent(object sender, LastTabReachedEventArgs e)
          {
  
      grd.CurrentCell = grd.Rows[x].Cells[2];
  }
  </code></pre>
  
  <p>But works if I select any additional columns:</p>
  
  <pre><code> private void grd_LastTabReachedEvent(object sender, LastTabReachedEventArgs e)
          {
       grd.CurrentCell = grd.Rows[x].Cells[3];
  }
  </code></pre>
  
  <p>I check the settings for the editable columns and they all have the same settings for a DataGridViewCell.</p>
  
  <p>Thanks Before Hand</p>
  
  <p><strong>Update #1</strong></p>
  
  <p>Something is really strange.  I tried using the name of the grid cell instead of the index number.  It selects the next column!  The layout of the fields shown is as follows:</p>
  
  <ol>
  <li>line number (read-only) dgvLineNum</li>
  <li>Contact Name (editable) dgvContactName</li>
  <li>Contact Phone (editable) dgvContactPhone</li>
  <li>Contact Email (editable) dgvContactEmail</li>
  </ol>
  
  <p>If I do the following to select the contact name on the new line:</p>
  
  <pre><code>grd.CurrentCell = grd.Rows[iNewLineNum - 1].Cells["dgvContactName"];
  </code></pre>
  
  <p>it selects (focuses) the next column dgvContactPhone.  </p>
  
  <p>If I do the following to select the contact phone on the new line:</p>
  
  <pre><code>grd.CurrentCell = grd.Rows[iNewLineNum - 1].Cells["dgvContactPhone"];
  </code></pre>
  
  <p>it selects the next column dgvContactEmail. </p>
  
  <p>However, if I select:</p>
  
  <pre><code>grd.CurrentCell = grd.Rows[iNewLineNum - 1].Cells["dgvLineNum"];
  </code></pre>
  
  <p>It does select (readonly selection) the first column (the line number).</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>I have placed a sample project with issue easily reproducible by pressing the tab key in the last column:</p>
  
  <p><a href="http://s000.tinyupload.com/index.php?file_id=01137880750225210507" rel="nofollow">http://s000.tinyupload.com/index.php?file_id=01137880750225210507</a></p>
  
  <p><strong>Solution</strong></p>
  
  <p>See solution below provided by LarsTech.  That solved it (return true instead of false in the ProcessDialogKey method):</p>
  
  <p>...</p>
  
  <pre><code>LastTabReachedEventArgs ev = new LastTabReachedEventArgs();
  ev.currentCol = currentCol;
  ev.currentRow = currentRow;
  OnLastTabReached(this, ev);
  return true;
  </code></pre>
  
  <p>...</p>