Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

c# - How to use Enter key as Tab key in DataGridView

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...