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
202 views
in Technique[技术] by (71.8m points)

c# - How to prevent going to next row after editing a DataGridViewTextBoxColumn and pressing EnterKey?

I'm working on a program with DataGridViews. In one DatagridView there is a DataGridViewTextBoxColumn, which is enabled to be edited by the user. When the user is done with typing the numbers into it, he presses ENTER on the keyboard. Now the DataGridView does all its Events, and after all Events, the last thing is the problem.

Everything is done, and Windows is going to Select the next DataGridViewRow, and I'm not able to prevent this.

I tried

if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true; // or e.Handled 

in nearly every event I found. Sadly I was only able to Prevent the ENTER key when the DataGridViewTextBoxColumn is not in edit mode.

Heres my methode t find the ENTER while in Editing

Adding the Event

private void dgr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress += new KeyPressEventHandler(dgr_KeyPress_NumericTester);
}

And this is the event to accept numeric input only.

private void dgr_KeyPress_NumericTester(object sender, KeyPressEventArgs e)
{
    if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8) e.Handled = true;
}

To explain in detail:

When the user enters a Value, that has some dependings, I would like to give another control the focus, so he is used to correct the dependings.

I also tried it with DependingControl.Focus() but the last "enter" is going to be the last thing on the view.

Does someone know how to prevent this?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I tried this for changing the Enter behaviour for your Grid by inheriting a customcolumn from Textbox column and overriding the below event

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

So instead of the Enter Key being sent it emulates the action for Tab which will move to the next cell. Hope this helps


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

...