在vb.net中使用Enter键移动到datagridview中的新行
这是我按下Enter键并移动到下一个单元格的代码:在vb.net中使用Enter键移动到datagridview中的新行,vb.net,datagridview,row,keypress,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
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
这是完美的工作,现在我想要的是,如果用户在最后一列中,我如何将单元格移动到第二行的第一列
谢谢。您可以在您的
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
在win窗体中的Datagridview处处理Enter键 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);
}
@matzone:您好,先生,您能解释一下我如何使用selectedcells吗?我不能清楚地理解您的评论,谢谢您的帮助对不起,它已经更正了。。我是说dgv.CurrentCell。。您尝试设置currentrow+1和column=0..@matzone:在哪一部分我将设置currentrow+1?我应该将其同时放在cellend edit和keydown中?