0

I want to catch the user hitting enter key when is in the cell edit mode. After that I am trying to show another form so the user can choose a value from it:

    private void dgDetalji_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter && dgDetalji.SelectedCells.Count == 1)
      {
        var selectedCell = dgDetalji.SelectedCells[0];
        if (selectedCell.OwningColumn.Name.ToLower() == "šifra")
        {
          using (ModalForm frm = new ModalForm())
          {
            var aaa = new ucEditor
            {
              Parent = frm,
              _query = "Select * From proizvodi",
              _where_statement = "where šifra like '{0}%'",
              _order_by_clause = "order by naziv"
            };
            frm.Controls.Add(aaa);
            aaa.tbSearch.Text = selectedCell.Value.ToString();
            aaa.setOleDbDataAdapter(connection);
            Utils.setEditor(aaa);
            aaa.Dock = DockStyle.Fill;
            frm._Click_refresh_on_load = true;
            frm.ShowDialog();
          }
        }
        e.Handled = true;
      }
    }

This only works when the cell is NOT in cell edit mode. How to catch ENTER key pressed in cell edit mode on datagridview?

UPDATE:

I will instruct the user to press CTRL + ENTER at the end of editing and then again press ENTER so dgDetalji_KeyDown will work.

Dejan Dozet
  • 948
  • 10
  • 26
  • 3
    See the [EditingControlShowing](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.editingcontrolshowing) event -- E.g., [Partially select the text of a DataGridView cell when clicked](https://stackoverflow.com/a/54777466/7444103) -- You can also provide your own `DataGridViewTextBoxColumn` and handle the TextBox directly. – Jimi Jul 10 '21 at 19:32
  • As a note, if you opt for a Custom Column, the ComponenetModel will acknowledge its existence and add its definition to the ComboBox selector (used to choose the Type of the Column) that appears when you add a new Column to a DataGridView in the Designer. As in, e.g., [Animate Images in DataGridView](https://stackoverflow.com/a/49742207/7444103) – Jimi Jul 10 '21 at 19:39
  • 1
    As Jimi suggest the grid’s `EditingControlShowing` event is the starting point. Since you are looking for the “Enter” key “while” a cell is in “edit mode”… you will need to wire up the grids `PreviewKeyDown` event to capture the “Enter” key. The `KeyDown` event will NOT fire if the “Enter” key is pressed while the cell is in “edit” mode. Take a look at Asaf’s answer in this SO question… [DataGridView keydown event not working in C#](https://stackoverflow.com/questions/4284370/datagridview-keydown-event-not-working-in-c-sharp) – JohnG Jul 10 '21 at 19:45
  • Don't most people that want to show some pick list of things in relation to a dgv cell simply use a DataGridViewComboBoxColumn rather than springing up an entirely new form to provide the picklist? – Caius Jard Jul 10 '21 at 19:58
  • oh, actually it doesnt work because in dgDetalji_EditingControlShowing I still don't have the value of the cell – Dejan Dozet Jul 10 '21 at 20:18
  • @JohnG As a *secondary note* :), you can subscribe to the `PreviewKeyDown` event of the EditingControl and remove the handler in `CellEndEdit` (of course you need a Field to assign the `e.Control` object in `EditingControlShowing`). This, if you don't use a Custom Column: in this, it's all much more straightforward, give that you can handle the TextBox directly. If micro-management of User input is required, this is the way to go, IMO. – Jimi Jul 10 '21 at 20:18
  • If you want to handle the `Enter` Key, the `KeyDown` event of the Cell's TextBox Control is not good. You need, as mentioned, the `PreviewKeyDown` event. The `Enter` key in a DGV terminates Edit Mode (thus the key doesn't reach the TextBox), while `SHIFT+Enter` creates a new line and this is acknowledged, since it doesn't terminate Edit Mode. – Jimi Jul 10 '21 at 20:20
  • From your comment… _”oh, actually it doesnt work because in dgDetalji_EditingControlShowing I still don't have the value of the cell”_ … this is true, all you want to do in the `EditingControlShowing` event is wire up the other events like the `KeyPressed` and or `PreviewKeyPressed` event. Those events will then fire when the user presses the keys while the cell is in edit mode. – JohnG Jul 10 '21 at 20:28
  • thanks, but I will not go so deep with this, 'cause I see it's not so easy, I will just instruct the user to press CTRL + ENTER and again ENTER to open a dialog to choose items from – Dejan Dozet Jul 10 '21 at 20:34

0 Answers0