5

I've made own datagridview control which ovveride OnKeyDown event:

public partial class PMGrid : DataGridView
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true; //suppress ENTER
            //SendKeys.Send("{Tab}"); //Next column (or row)
            base.OnKeyDown(e);
        }
        else if (e.KeyCode == Keys.Tab)
        {
            base.OnKeyDown(e);
            this.BeginEdit(false);
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
}

When I click on datagridview and press Enter it works perfect because row isn't changed and KeyUp event is fired. But when I press Tab, next cell is selected and it is changed to EditMode. And when I press Enter in this cell KeyUp event isn't fired and KeyPress too. I try to do that user can move from one cell to next cell and then user can write something in this cell and then when user press Enter this value is saved to the database. But when cell is in EditMode I cannot detect that user press Enter.

Thanks

1 Answer 1

6

You should call the KeyPress or KeyUp event in the EditingControlShowing event of the datagridview. Something like this should work:

private void dtgrd1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    var txtEdit = (TextBox)e.Control;
    txtEdit.KeyPress += EditKeyPress; //where EditKeyPress is your keypress event
}


private void EditKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true; //suppress ENTER
                //SendKeys.Send("{Tab}"); //Next column (or row)
                base.OnKeyDown(e);
            }
            else if (e.KeyCode == Keys.Tab)
            {
                base.OnKeyDown(e);
                this.BeginEdit(false);
            }
            else
            {
                base.OnKeyDown(e);
            }

}

Let me know if you have any doubts while implementing this code.

EDIT

To avoid going to the next row on enter, please check this resolved question: How to prevent going to next row after editing a DataGridViewTextBoxColumn and pressing EnterKey?

10
  • I changed code according to your instrucionts. But this code catch all pressed keys exept for Enter. When I press Enter both of these events are not fired. And there is one problem yet. When I press Enter and cell is EditMode selection of the row is changed to the next row. As you can see in OnKeyDown I have e.SuppressKeyPress = true to prevent changing selection of the row when user press Enter.
    – Robert
    Dec 16, 2011 at 18:20
  • @Robert I have not made any changes to the code. Just changed the event that you need to call the code from. I am a bit confused in the logic of your code. Could you explain?
    – reggie
    Dec 16, 2011 at 18:33
  • @Robert if you just want to save the data into the database on enter, just execute the save code on enter. Have updated the code
    – reggie
    Dec 16, 2011 at 18:38
  • NO. I can't just save data on enter. When user press enter, selection of the row is changed to the next row. So I've created my own grid which inherits from DataGridView and override OnKeyDown and suppress Enter key. And this works perfectly. Now when user press enter this same row is still selected. But the problem is when user click into cell, then I can't catch pressing of Enter and selection of the row again changing to the next row. Here it is explained: csharp411.com/enter-key-in-datagridview. It works but only when cell isn't in editmode.
    – Robert
    Dec 16, 2011 at 19:44
  • 1
    @Robert Why don't you call the EditingControlShowing even on the click event?
    – reggie
    Dec 16, 2011 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.