locked
prevent selecting next row when hitting enter key in datagridview? RRS feed

  • Question

  • how would I keep the current row selected after hitting enter on the row?
    Friday, March 31, 2006 4:12 PM

Answers

  • Hi Suedueno,

    You can derive from DataGridView and override OnKeyDown:
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if ((e.KeyData & Keys.KeyCode) == Keys.Enter)
                    return;
                else
                    base.OnKeyDown(e);
            }

    The control won't go to the next row if the current cell isn't in editing mode.
    If you hit enter while there is an editing control, the cell will still be committed and the control will go to the next row.
    Because it is the Editing control accept the Enter Key.

    Here is link about DataGrid for your reference.
    http://www.msnewsgroups.net/link.aspx?url=http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang

    Tuesday, April 4, 2006 3:32 AM

All replies

  • Hi Suedueno,

    You can derive from DataGridView and override OnKeyDown:
            protected override void OnKeyDown(KeyEventArgs e)
            {
                if ((e.KeyData & Keys.KeyCode) == Keys.Enter)
                    return;
                else
                    base.OnKeyDown(e);
            }

    The control won't go to the next row if the current cell isn't in editing mode.
    If you hit enter while there is an editing control, the cell will still be committed and the control will go to the next row.
    Because it is the Editing control accept the Enter Key.

    Here is link about DataGrid for your reference.
    http://www.msnewsgroups.net/link.aspx?url=http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang

    Tuesday, April 4, 2006 3:32 AM
  • THanks. I just tried the following and it worked as well. The grid won't be editable so I didn't have to worry about that.

     

    Private Sub dg1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg1.KeyDown

    If e.KeyCode = Keys.Enter Then

    e.SuppressKeyPress = True

    End If

    End Sub

    • Proposed as answer by first2018 Tuesday, April 14, 2020 12:45 AM
    Tuesday, April 4, 2006 12:50 PM
  • Hi Suedueno,

    Thank you for your reply and the detailed additional feedback on how you were successful in resolving this issue. This information has been added to Microsoft's database. Your solution will benefit many other users, and we really value having you as a Microsoft customer.

    If you have any other questions or concerns, please do not hesitate to contact us. It is always our pleasure to be of assistance.

    Have a nice day!

    Best regards,

    Peter Huang

    Wednesday, April 5, 2006 2:25 AM
  • This code prevent selecting the next row when hitting enter key.

     

    But I want that :

     

    I want to open a new form based on the selected row. So When I press the enter key, it should prevent selecting the next row and also should open the other form based on the selected row.

     

    Please suggest how to achieve this. The code you posted i.e.

     

    If e.KeyCode = Keys.Enter Then

    e.SuppressKeyPress = True

    End If

     

    prevent selecting the next row when hitting enter key. But tell me then how to open new form based on the selected row

     

    Pl suggest

    Tuesday, November 13, 2007 1:29 PM
  •             if (e.KeyCode == Keys.Enter)
                {
                    // supress the enter key and call a button's click event
                    e.SuppressKeyPress = true;
                    mSelect_Click(null, EventArgs.Empty);

                    // you can set return value in mSelect_Click method and you can use that value in the parent where you called this form from
                }

    Sunday, August 31, 2008 7:59 AM