我有一个包含5列的DataGridView
。如果在第一列中按Enter键,焦点将移至下一行。当我按下Enter键时,我想将焦点移到下一个列。
private void dgvComp_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dgvComp.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
{
SendKeys.Send("{tab}");
}
}
在上面的代码中,我将列2,3和4作为只读列。如果按Tab键,焦点应直接转到第5列。
我该怎么做?
答案 0 :(得分:8)
你会试试这个解决方案
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);
}
}
答案 1 :(得分:0)
尝试使用KeyPress事件处理程序:
private void dgvComp_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Enter:
SendKeys.Send("{Tab}");
break;
default:
break;
}
}
答案 2 :(得分:0)
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];
}
}