How to move from one column to another of a DataGridView by pressing enter

0

Friends again I come for help.

I commented that I am working on a Form that contains a DataGridView in which I must control a data entry sequence to said DataGridView , but I need to follow the sequence from left to right, now my problem , if I press "Enter" the active cell changes to the one below and this is not what I need. What I need is that when I finish editing the cell with a Tab or a Enter go to the next column

Any suggestions on how I could control that? Thank you very much

So far I have tried two ways that I found on the Internet

1.- The first one, a bit basic but it does not work for me, in the KeyDown event of the DataGridView

    private void dtVentas_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = true;
            SendKeys.Send("{TAB}");
            MessageBox.Show("Deberia pasar a la siguiete celda");

        }

2.- The second one is: With the EditingControlShowing method of the DataGridView and in this event I call the KeyPress event of the same table and in the latter I validate the key that I press but it does not work either. Data Apart, in this same method I perform the validations of what kind of data I can enter in the cell of the table, I do not know if that is relevant.

Code:

    private void dtVentas_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {

        DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)e.Control;

        dText.KeyPress -= new KeyPressEventHandler(dtVentas_KeyPress);
        dText.KeyPress += new KeyPressEventHandler(dtVentas_KeyPress);
    }

KeyPress Event:

     private void dtVentas_KeyPress(object sender, KeyPressEventArgs e)
    {
        //
        switch (this.dtVentas.CurrentCell.ColumnIndex)
        {
            case 0:// CASO 0 PARA EL CODIGO DEL ITEM
                //MessageBox.Show("Llegue al evento keypress");
                if (e.KeyChar == (char)Keys.Enter)
                {
                    MessageBox.Show("Di un enter en la tabla");
                    SendKeys.Send("{UP}");
                    SendKeys.Send("{TAB}");
                }
                else
                {
                    this.dtVentas.Rows[this.dtVentas.CurrentRow.Index].ErrorText = "Solo Numeros en la Columna 'Codigo Item'";
                    e.Handled = Obj.SoloNumeros(e);
                }




                break;
        }
    }
    
asked by Benjamin Garrido H. 01.08.2017 в 22:02
source

2 answers

0

You can do it by controlling it from the event KeyDown of the dataGrid, what you have to do is that when you press the key you want change the CurrentCell of the datagrid you want, an example would be something like this:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = true;
    int numColumn = dataGridView1.CurrentCell.ColumnIndex;
    int numRow = dataGridView1.CurrentCell.RowIndex;
    if (numColumn == dataGridView1.Columncount-1)
    {
        if (dataGridView1.RowCount > (numRow + 1))
        {
            dataGridView1.CurrentCell = dataGridView1[1, numRow + 1];
        }           
    }
    else
        dataGridView1.CurrentCell = dataGridView1[numColumn + 1, numRow];
}
    
answered by 02.08.2017 в 10:09
0

I use Event KeyPress and EditingControlShowing of Datagridview and it worked for me.

  

But I noticed that in the event EditingControlShowing of your question you used this: DataGridViewTextBoxEditingControl dText ... , I use it in the following way: TextBox textbox = e.Control as TextBox; and it works for me.

Take a look:

private void dtVentas_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
       TextBox textbox = e.Control as TextBox;
       if (textbox != null)
       {
             textbox.KeyPress -= new KeyPressEventHandler(dtVentas_KeyPress);
             textbox.KeyPress += new KeyPressEventHandler(dtVentas_KeyPress);
        }
}

Event KeyPress :

private void dtVentas_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13) // Si es un enter
    {
        e.Handled = true; //Interceptamos la pulsación
        SendKeys.Send("{TAB}"); //Pulsamos la tecla Tabulador por código
    }
}
  

The answer is a bit late, but try anyway. Regards!

    
answered by 08.01.2018 в 16:29