Matthew
Matthew

Reputation: 640

Move to the new row in datagridview using Enter Key in vb.net

This is my code for pressing Enter Key and moving to the next cell:

Private Sub dvFromAlloc_CellEndEdit(ByVal sender As Object, ByVal e As   System.Windows.Forms.DataGridViewCellEventArgs) Handles dvFromAlloc.CellEndEdit
    SendKeys.Send("{up}")
    SendKeys.Send("{right}")
End Sub

Private Sub dvFromAlloc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dvFromAlloc.KeyDown
    If e.KeyCode = Keys.Enter Then
        SendKeys.Send("{up}")
        SendKeys.Send("{right}")
    End If
End Sub

This is perfectly working, now what I want is if the user is in the last column, how can I move the cell to the first column of the second row?

Thank you.

Upvotes: 0

Views: 9147

Answers (2)

matzone
matzone

Reputation: 5719

You may try this in your KeyDown event ..

Private Sub dvFromAlloc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dvFromAlloc.KeyDown
    If e.KeyCode = Keys.Enter Then

        dvFromAlloc.CurrentCell = dvFromAlloc.Rows(dvFromAlloc.CurrentCell.RowIndex + 1).Cells(0)
        dtg.Rows(i).Cells(aColumn(x))
    End If
End Sub

Upvotes: 1

Nagarjun G N
Nagarjun G N

Reputation: 39

Handle Enter Key at Datagridview in win forms..

bool notlastColumn = true;

    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        int icolumn = dataGridView1.CurrentCell.ColumnIndex;
        int irow = dataGridView1.CurrentCell.RowIndex;
        int i = irow;
        if (keyData == Keys.Enter)
        {
            if (icolumn == dataGridView1.Columns.Count - 1)
            {

                //dataGridView1.Rows.Add();
                if (notlastColumn == true  )
                {
                   dataGridView1.CurrentCell= dataGridView1.Rows[i].Cells[0];
                }
                dataGridView1.CurrentCell = dataGridView1[0, irow + 1];

            }
            else
            {
                dataGridView1.CurrentCell = dataGridView1[icolumn + 1, irow];
            }
            return true;
        }
        else
            return base.ProcessCmdKey(ref msg, keyData);
    }

Upvotes: 0

Related Questions