使用vb.net中的Enter键移至datagridview中的新行

时间:2013-09-05 06:22:15

标签: vb.net datagridview row keypress

这是我按下Enter键并移动到下一个单元格的代码:

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

这完全有效,现在我想要的是如果用户在最后一列,我该如何将单元格移动到第二行的第一列?

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以在KeyDown活动中尝试此操作..

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

答案 1 :(得分:1)

在赢取表单中处理Datagridview的输入键..

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);
    }