[英]How to use Enter key as Tab key in DataGridView
I have a DataGridView
with 5 columns. 我有一个5列的
DataGridView
。 If press Enter key in the first column, focus moves to next row. 如果在第一列中按Enter键,焦点将移至下一行。 I want to move the focus to the next column when I press Enter key.
当我按下Enter键时,我想将焦点移到下一列 。
private void dgvComp_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dgvComp.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
{
SendKeys.Send("{tab}");
}
}
In the above code I have columns 2,3 and 4 as read only columns. 在上面的代码中,我将列2,3和4作为只读列。 If I press Tab, focus should directly go to 5th column.
如果按Tab键,焦点应直接转到第5列。
How can I do that? 我怎样才能做到这一点?
would you pls try this solution 你会试试这个解决方案吗?
using System.Diagnostics;
class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDataGridViewKey(e);
}
}
Try using a KeyPress event handler: 尝试使用KeyPress事件处理程序:
private void dgvComp_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Enter:
SendKeys.Send("{Tab}");
break;
default:
break;
}
}
datagridview.EditingControlsShowing += datagridview_EditingControls;
datagridview_EditingControls(object sender,DataGridViewEditingControlShowingEventArgs e)
{
e.control.KeyDown += Control_KeyDown;
}
private void Control_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
dataGridView.CurrentCell = dataGridView.CurrentRow.Cells[e.ColumnIndex + 1];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.