Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a datagridview control(DGV) with 3 columns, all are with editing control of type textbox.

Let me explain the two scenarios:
First, in the very first column, autocomplete functionality is switched on. When I start typing autocomplete suggests me for some options and completes it as soon as I press the Enter key.
After pressing the enter key, the focus shifts to the cell in the same column in the next row, just below it.

Second, for the cells where autocomplete functionality is not switched on, on pressing enter key, focus shifts to the next cell in the same row. This functionality is provided by my side as you will find in the code below.

Now, my requirement is to shift focus to the next cell in the same row on enter key press regardless autocomplete functionality is switched on or off for a cell.
But this isn't happening for the cells in which autocomplete in on.

Guide me how to achieve it.

C#
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
          try
          {
              TextBox _tbDrCr = e.Control as TextBox;
              if (_tbDrCr != null)
              {
                  switch (((DataGridView)sender).CurrentCell.OwningColumn.Name)
                  {
                      case "nature":
                          _tbDrCr.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                          _tbDrCr.AutoCompleteSource = AutoCompleteSource.CustomSource;
                          _tbDrCr.AutoCompleteCustomSource = InitializeItemCollection(new String[] { "Dr", "Cr" });
                          _tbDrCr.KeyDown += _tbDrCr_KeyDown;
                          break;
                      case "nature_debit":
                      case "nature_credit":
                          _tbDrCr.KeyPress += _tbDrCr_KeyPress;
                          break;
                      default:
                          _tbDrCr.AutoCompleteMode = AutoCompleteMode.None;
                          break;
                  }
              }
              else
                  throw new InvalidCastException();
          }
          catch (InvalidCastException ex)
          {
              MessageBox.Show(ex.Message+ "\n"+ e.Control.GetType().ToString() +" to TextBox");
          }
      }




C#
void _tbDrCr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (dataGridView1.CurrentCell.ColumnIndex < dataGridView1.Columns.Count - 1)
                {
                    dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex + 1, dataGridView1.CurrentRow.Index];
                    dataGridView1.CurrentCell.Style.BackColor = Color.Yellow;
                }
        }



C#
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
           System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace();
            MessageBox.Show(stack.ToString());
1].Value.ToString());
}

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
           switch (e.KeyCode)
           {
               case Keys.Enter:
                   if (dataGridView1.CurrentCell.ColumnIndex < dataGridView1.Columns.Count - 1)
                   {
                       dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex + 1, dataGridView1.CurrentRow.Index];
                       dataGridView1.CurrentCell.Style.BackColor = Color.Yellow;
                   }

                   break;
           }
}



C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
            if (dataGridView1.CurrentCell.ColumnIndex < dataGridView1.Columns.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex + 1, dataGridView1.CurrentRow.Index];
                dataGridView1.CurrentCell.Style.BackColor = Color.Yellow;
            }                    
}


What I have tried:

I tried to capture enter key at:
1. Cell's key down event,
2. DGV's key down event,
3. DGV's preview key down event,
4. DGV's cellleave event,
5. DGV's cellendedit event, and after handling at all these events, the execution is still going through some more events that are unknown to me.

I went through tracing the stack and the depth that I achieved was upto CellEndEdit() method for the DGV and afterwards I am unable to find out what is the thing that is shifting focus to new row. In cellendedit(), I tried to shift focus to next cell instead of row but this was overridden by some other code in the forward steps that I couldn't trace.

UPDATE 1:
After posting this que, I was trying to look for the solution and I got this thing to work but the solution doesn't looks to me a good one.
I tries handling enter key press in PreviewKeyDown event for the textboxcell, and things start working the way required.

But, as of my knowledge, previewkeydown is the event that occurs before keydown event. If it's working at previewkeydown, same functionality must also work within keydown since enter key is being captured at keydown event too. But somehow after capturing the key press and keydown event put focus on the required cell, some other thing is there which is again shifting the focus to the next row.


UPDATE 2:
After posting update 1, I was trying and I found that KeyDown event is not fired up for the Enter key press on the textbox cell with autocomplete switched on. I tried setting isinputkey to true for enter key press at previewkeydown event for the cell but again keydown event was not fired.
Now, I only need to fire keydown event for enter key press for the functionality to start working. Any suggestions?
Posted
Updated 14-Jul-16 21:20pm
v5
Comments
Member 13566383 1-Apr-21 14:39pm    
I just encountered the same problem. Did anybody find a better solution in the meantime?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900