C# 如何在 Enter 按键事件上将焦点移到 datagridview 中的下一个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9666657/
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
How to move focus on next cell in a datagridview on Enter key press event
提问by Sukanya
Friends, I'm working on windows application using C#. I'm using a datagridview to display records. The functionality I need is when I press "Enter" key the focus should go to the next cell(column of same row). If it's the last column in the grid then the focus should go to the first column of the next row. I've already tried using
朋友们,我正在使用 C# 开发 Windows 应用程序。我正在使用 datagridview 来显示记录。我需要的功能是当我按“Enter”键时,焦点应该转到下一个单元格(同一行的列)。如果它是网格中的最后一列,则焦点应转到下一行的第一列。我已经尝试使用
SendKeys.Send("{Tab}")
in datagridview1_KeyDown and datagridview1_KeyPress event. But focus is moving diagonally down. Please help me to solve this.
在 datagridview1_KeyDown 和 datagridview1_KeyPress 事件中。但焦点正在对角线向下移动。请帮我解决这个问题。
采纳答案by Kishore Kumar
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
int iColumn = dataGridView1.CurrentCell.ColumnIndex;
int iRow = dataGridView1.CurrentCell.RowIndex;
if (iColumn == dataGridView1.Columncount-1)
{
if (dataGridView1.RowCount > (iRow + 1))
{
dataGridView1.CurrentCell = dataGridView1[1, iRow + 1];
}
else
{
//focus next control
}
}
else
dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
}
回答by Amritpal Singh
you can use selectionchanged event of the datagridview. In your form
您可以使用 datagridview 的 selectionchanged 事件。在你的表格中
private DataGridViewCell _celWasEndEdit;
private void datagridview_SelectionChanged(object sender, EventArgs e)
{
if (MouseButtons != 0) return;
if (_celWasEndEdit != null && datagridview.CurrentCell != null)
{
// if we are currently in the next line of last edit cell
if (datagridview.CurrentCell.RowIndex == _celWasEndEdit.RowIndex + 1 &&
datagridview.CurrentCell.ColumnIndex == _celWasEndEdit.ColumnIndex)
{
int iColNew;
int iRowNew = 0;
if (_celWasEndEdit.ColumnIndex >= datagridview.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dgvItems.CurrentCell.RowIndex;
}
else
{
iColNew = _celWasEndEdit.ColumnIndex + 1;
iRowNew = _celWasEndEdit.RowIndex;
}
datagridview.CurrentCell = datagridview[iColNew, iRowNew];
}
}
_celWasEndEdit = null;
}
private void datagridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
_celWasEndEdit = dgvItems[e.ColumnIndex, e.RowIndex];
}
回答by YOusaFZai
bool notlastColumn =true; //class level variable--- to check either last column is reached or not
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.ColumnCount - 1 == e.ColumnIndex) //if last column
{
KeyEventArgs forKeyDown = new KeyEventArgs(Keys.Enter);
notlastColumn = false;
dataGridView1_KeyDown(dataGridView1, forKeyDown);
}
else
{
SendKeys.Send("{up}");
SendKeys.Send("{right}");
}
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && notlastColumn) //if not last column move to nex
{
SendKeys.Send("{up}");
SendKeys.Send("{right}");
}
else if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{home}");//go to first column
notlastColumn = true;
}
}
回答by Arun Agarwal
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;
if (keyData == Keys.Enter)
{
if (icolumn == dataGridView1.Columns.Count - 1)
{
dataGridView1.Rows.Add();
dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
}
else
{
dataGridView1.CurrentCell = dataGridView1[icolumn + 1, irow];
}
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}
}
回答by Rijul Sudhir
Even though the codes I tried moved the focus to next cell, They where effecting the mouse click when the cell was in edit mode. This is what I came up with in the end.
即使我尝试的代码将焦点移到下一个单元格,当单元格处于编辑模式时它们会影响鼠标单击。这就是我最终想到的。
bool _dgv_list_cellEndEdit = false; // class level variable
void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress=true;
SendKeys.Send("{Tab}");
}
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
_dgv_list_cellEndEdit=true;
}
void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
_dgv_list_cellEndEdit=false;
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(_dgv_list_cellEndEdit)
{
_dgv_list_cellEndEdit=false;
SendKeys.Send("{Up}");
SendKeys.Send("{Tab}");
}
}

