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

Robert
  • 2,571
  • 10
  • 63
  • 95

1 Answers1

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?

Community
  • 1
  • 1
reggie
  • 13,313
  • 13
  • 41
  • 57
  • 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 '11 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 '11 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 '11 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: http://www.csharp411.com/enter-key-in-datagridview/. It works but only when cell isn't in editmode. – Robert Dec 16 '11 at 19:44
  • 1
    @Robert Why don't you call the EditingControlShowing even on the click event? – reggie Dec 16 '11 at 19:52
  • I have this event too :protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e) { var txtEdit = (TextBox)e.Control; txtEdit.KeyDown += new KeyEventHandler(txtEdit_KeyDown); txtEdit.KeyPress += new KeyPressEventHandler(txtEdit_KeyPress); } but what it helps ? When I press Enter next row is selected, I want to the same row was selected after enter pressing. – Robert Dec 16 '11 at 20:05
  • @Robert A similar question has been already resolved. I have updated my answer with a link to the resolved question. – reggie Dec 16 '11 at 20:11
  • Sorry, I read comments and no longer looked at the first answer. It works, thanks :) – Robert Dec 17 '11 at 06:23
  • It seems as if KeyPress and KeyDown are being mixed in the code; is this intentional? And SuppressKeyPress is not recognized in KeyPress. – B. Clay Shannon-B. Crow Raven Sep 26 '12 at 17:14
  • Didn't work for me. Had to replace KeyCode with KeyChar to compile. – Arvind Nov 19 '19 at 18:32