How to use Enter key as Tab key in DataGridView

大城市里の小女人 提交于 2019-12-18 09:14:47

问题


I have a DataGridView with 5 columns. If press Enter key in the first column, focus moves to next row. I want to move the focus to the next column when I press Enter key.

 private void dgvComp_CellEnter(object sender, DataGridViewCellEventArgs e)
 {
    if (dgvComp.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
    {
       SendKeys.Send("{tab}");

    }   
 }

In the above code I have columns 2,3 and 4 as read only columns. If I press Tab, focus should directly go to 5th column.

How can I do that?


回答1:


would you pls try this solution

using System.Diagnostics;
class MyDataGridView : DataGridView
{

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Enter) {
            base.ProcessTabKey(Keys.Tab);
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }

    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) {
            base.ProcessTabKey(Keys.Tab);
            return true;
        }
        return base.ProcessDataGridViewKey(e);
    }

}



回答2:


Try using a KeyPress event handler:

private void dgvComp_KeyPress(object sender, KeyPressEventArgs e)
{
    switch (e.KeyChar)
    {
        case (char)Keys.Enter:
            SendKeys.Send("{Tab}");
            break;
        default:
            break;
    }
}



回答3:


datagridview.EditingControlsShowing += datagridview_EditingControls;
datagridview_EditingControls(object sender,DataGridViewEditingControlShowingEventArgs e)
{
  e.control.KeyDown += Control_KeyDown;
}
private void Control_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            dataGridView.CurrentCell = dataGridView.CurrentRow.Cells[e.ColumnIndex + 1];
        }
    }


来源:https://stackoverflow.com/questions/15499282/how-to-use-enter-key-as-tab-key-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!