在 vb.net 中使用 Enter 键移动到 datagridview 中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18629095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move to the new row in datagridview using Enter Key in vb.net
提问by Matthew
This is my code for pressing Enter Key and moving to the next cell:
这是我按 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
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.
谢谢你。
回答by matzone
You may try this in your KeyDownevent ..
你可以在你的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
回答by Nagarjun G N
Handle Enter Key at Datagridview in win forms..
在 Win 表单中处理 Datagridview 中的 Enter Key..
bool notlastColumn = true;
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);
}

