Bypass read only cells in DataGridView when pressing TAB key

前端 未结 8 671
粉色の甜心
粉色の甜心 2020-12-31 18:07

Can anyone show me some code of how I could bypass read only cells in DatagridView when pressing TAB key?

相关标签:
8条回答
  • 2020-12-31 18:29
     if (e.KeyValue == 13)
            {
                e = new KeyEventArgs(Keys.Tab);
            }
    
    0 讨论(0)
  • 2020-12-31 18:35

    Hector - I hope you are still listening. Your solution is the most elegant and straightforward one I've seen in extensive searching. I've run into the suggestion to override the key events, but your suggestion to invoke base.ProcessTabKey is especially simple and it handles focus passing to the next control when you reach the end of the dgv. The one additional thing your solution needed was a check in MyProcessTabKey for the last cell of the dgv. If that cell is readonly and the user tabs in the previous cell, the while statement goes into an infinite loop. Adding the following code as the first statement in the while loop seems to solve the problem, though it does leave the last (readonly) cell the "active" cell (meaning it appears as though it's selected).

        if (this.CurrentCell.RowIndex == this.Rows.Count - 1
                    && this.CurrentCell.ColumnIndex == this.Columns.Count - 1)
                    { retValue = false; break; } 
    

    I have a follow-on question. Do you or anyone else know how to make this approach work with Shift-Tab, so the dgv skips readonly cells in a backward direction as well? Since Shift and Tab are different key events, I don't know how to detect Shift-Tab in the overridden ProcessDialogKey and ProcessDataGridViewKey methods. Thanks. Steve

    0 讨论(0)
提交回复
热议问题