如何在 DataGridView c# 中编辑单元格后使用回车键
how to use enter key after cell edit in DataGridView c#
我在单元格中键入内容后尝试转到 datagridview 中的下一个单元格,但它转到同一列中的下一行
** 如果不在单元格中输入任何内容,它工作正常
怎么解决
private void dgvacc_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int row = dgvacc.CurrentCell.RowIndex;
int col = dgvacc.CurrentCell.ColumnIndex;
try
{
if (col < dgvacc.Columns.Count - 1)
{
dgvacc.CurrentCell = dgvacc.Rows[row -1].Cells[col + 1];
dgvacc.Focus();
}
else if (col == dgvacc.Columns.Count - 1)
{
dgvacc.CurrentCell = dgvacc.Rows[row].Cells[0];
dgvacc.Focus();
}
}
catch (Exception)
{
}
}
}
选项 1
重写表单的 ProcessCmdKey
以捕获 Keys.Enter
按键和 select 下一个单元格 IF 网格被聚焦 OR DataGridView.EditingControl
属性 不是 null/Nothing
.
public partial class YourForm : Form
{
public YourForm()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((dataGridView1.Focused || dataGridView1.EditingControl != null)
&& keyData == Keys.Enter)
{
SelectNextCell();
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}
private void SelectNextCell()
{
var col = dataGridView1.CurrentCell.ColumnIndex;
var row = dataGridView1.CurrentCell.RowIndex;
if (col < dataGridView1.ColumnCount - 1) col++;
else
{
col = 0;
row++;
}
// Optional, select the first cell if the current cell is the last one.
// You can return; here instead or add new row.
if (row == dataGridView1.RowCount) row = 0;
dataGridView1.CurrentCell = dataGridView1[col, row];
}
}
选项 2
从 DataGridView
派生一个新的 class 并执行相同的操作,除了 focus 和 EditingControl 检查:
[DesignerCategory("Code")]
public class DataGridViewEx : DataGridView
{
public DataGridViewEx() : base() { }
// Optional to enable/disable this feature...
public bool SelectNextCellOnEnter { get; set; } = true;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (SelectNextCellOnEnter && keyData == Keys.Enter)
{
SelectNextCell();
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
private void SelectNextCell()
{
var col = CurrentCell.ColumnIndex;
var row = CurrentCell.RowIndex;
if (col < ColumnCount - 1) col++;
else
{
col = 0;
row++;
}
if (row == RowCount) row = 0;
CurrentCell = this[col, row];
}
}
感谢大家
我找到了答案并解决了它 - 再次感谢
private DataGridViewCell dgvEndEditCell;
private bool _EnterMoveNext = true;
[System.ComponentModel.DefaultValue(true)]
public bool OnEnterKeyMoveNext
{
get
{
return this._EnterMoveNext;
}
set
{
this._EnterMoveNext = value;
}
}
private void dgvacc_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
int iColumn = dgvacc.CurrentCell.ColumnIndex;
int iRow = dgvacc.CurrentCell.RowIndex;
if (iColumn == dgvacc.Columns.Count - 1 && iRow != dgvacc.Rows.Count - 1)
{
dgvacc.CurrentCell = dgvacc[0, iRow + 1];
}
else if (iColumn == dgvacc.Columns.Count - 1 && iRow == dgvacc.Rows.Count - 1)
{
}
else
{
dgvacc.CurrentCell = dgvacc[iColumn + 1, iRow];
}
}
}
private void dgvacc_SelectionChanged(object sender, EventArgs e)
{
if (this._EnterMoveNext && MouseButtons == 0)
{
if (this.dgvEndEditCell != null && dgvacc.CurrentCell != null)
{
if (dgvacc.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex + 1
&& dgvacc.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
{
int iColNew;
int iRowNew;
if (this.dgvEndEditCell.ColumnIndex >= dgvacc.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dgvacc.CurrentCell.RowIndex;
}
else
{
iColNew = this.dgvEndEditCell.ColumnIndex + 1;
iRowNew = this.dgvEndEditCell.RowIndex;
}
dgvacc.CurrentCell = dgvacc[iColNew, iRowNew];
}
}
this.dgvEndEditCell = null;
}
}
我在单元格中键入内容后尝试转到 datagridview 中的下一个单元格,但它转到同一列中的下一行 ** 如果不在单元格中输入任何内容,它工作正常 怎么解决
private void dgvacc_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int row = dgvacc.CurrentCell.RowIndex;
int col = dgvacc.CurrentCell.ColumnIndex;
try
{
if (col < dgvacc.Columns.Count - 1)
{
dgvacc.CurrentCell = dgvacc.Rows[row -1].Cells[col + 1];
dgvacc.Focus();
}
else if (col == dgvacc.Columns.Count - 1)
{
dgvacc.CurrentCell = dgvacc.Rows[row].Cells[0];
dgvacc.Focus();
}
}
catch (Exception)
{
}
}
}
选项 1
重写表单的 ProcessCmdKey
以捕获 Keys.Enter
按键和 select 下一个单元格 IF 网格被聚焦 OR DataGridView.EditingControl
属性 不是 null/Nothing
.
public partial class YourForm : Form
{
public YourForm()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((dataGridView1.Focused || dataGridView1.EditingControl != null)
&& keyData == Keys.Enter)
{
SelectNextCell();
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}
private void SelectNextCell()
{
var col = dataGridView1.CurrentCell.ColumnIndex;
var row = dataGridView1.CurrentCell.RowIndex;
if (col < dataGridView1.ColumnCount - 1) col++;
else
{
col = 0;
row++;
}
// Optional, select the first cell if the current cell is the last one.
// You can return; here instead or add new row.
if (row == dataGridView1.RowCount) row = 0;
dataGridView1.CurrentCell = dataGridView1[col, row];
}
}
选项 2
从 DataGridView
派生一个新的 class 并执行相同的操作,除了 focus 和 EditingControl 检查:
[DesignerCategory("Code")]
public class DataGridViewEx : DataGridView
{
public DataGridViewEx() : base() { }
// Optional to enable/disable this feature...
public bool SelectNextCellOnEnter { get; set; } = true;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (SelectNextCellOnEnter && keyData == Keys.Enter)
{
SelectNextCell();
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
private void SelectNextCell()
{
var col = CurrentCell.ColumnIndex;
var row = CurrentCell.RowIndex;
if (col < ColumnCount - 1) col++;
else
{
col = 0;
row++;
}
if (row == RowCount) row = 0;
CurrentCell = this[col, row];
}
}
感谢大家 我找到了答案并解决了它 - 再次感谢
private DataGridViewCell dgvEndEditCell;
private bool _EnterMoveNext = true;
[System.ComponentModel.DefaultValue(true)]
public bool OnEnterKeyMoveNext
{
get
{
return this._EnterMoveNext;
}
set
{
this._EnterMoveNext = value;
}
}
private void dgvacc_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
int iColumn = dgvacc.CurrentCell.ColumnIndex;
int iRow = dgvacc.CurrentCell.RowIndex;
if (iColumn == dgvacc.Columns.Count - 1 && iRow != dgvacc.Rows.Count - 1)
{
dgvacc.CurrentCell = dgvacc[0, iRow + 1];
}
else if (iColumn == dgvacc.Columns.Count - 1 && iRow == dgvacc.Rows.Count - 1)
{
}
else
{
dgvacc.CurrentCell = dgvacc[iColumn + 1, iRow];
}
}
}
private void dgvacc_SelectionChanged(object sender, EventArgs e)
{
if (this._EnterMoveNext && MouseButtons == 0)
{
if (this.dgvEndEditCell != null && dgvacc.CurrentCell != null)
{
if (dgvacc.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex + 1
&& dgvacc.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
{
int iColNew;
int iRowNew;
if (this.dgvEndEditCell.ColumnIndex >= dgvacc.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dgvacc.CurrentCell.RowIndex;
}
else
{
iColNew = this.dgvEndEditCell.ColumnIndex + 1;
iRowNew = this.dgvEndEditCell.RowIndex;
}
dgvacc.CurrentCell = dgvacc[iColNew, iRowNew];
}
}
this.dgvEndEditCell = null;
}
}