各位好,我正在使用 C# 开发 Windows 应用程序。我正在使用一个 datagridview 来显示记录。我需要的功能是当我按下 "Enter" 键时,焦点应该转移到下一个单元格(同一行的列)。如果它是网格中的最后一列,则焦点应该转移到下一行的第一列。我已经尝试过使用
SendKeys.Send("{Tab}")
在datagridview1_KeyDown和datagridview1_KeyPress事件中,但焦点会对角线向下移动。请帮我解决这个问题。
各位好,我正在使用 C# 开发 Windows 应用程序。我正在使用一个 datagridview 来显示记录。我需要的功能是当我按下 "Enter" 键时,焦点应该转移到下一个单元格(同一行的列)。如果它是网格中的最后一列,则焦点应该转移到下一行的第一列。我已经尝试过使用
SendKeys.Send("{Tab}")
在datagridview1_KeyDown和datagridview1_KeyPress事件中,但焦点会对角线向下移动。请帮我解决这个问题。
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];
}
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);
}
}
您可以在表单中使用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];
}
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;
}
}
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}");
}
}
hidden(Visible=false)
列,那么@Amritpal Singhs的解决方案会抛出错误。下面的解决方案考虑了这些隐藏的列,并且可以正常工作:private void gridDetails_SelectionChanged(object sender, EventArgs e)
{
if (MouseButtons != 0) return;
if (_lastCellEndEdit != null && gridDetails.CurrentCell != null)
{
// if we are currently in the next line of last edit cell
if (gridDetails.CurrentCell.RowIndex == _lastCellEndEdit.RowIndex + 1 &&
gridDetails.CurrentCell.ColumnIndex == _lastCellEndEdit.ColumnIndex)
{
int iColNew;
int iRowNew = 0;
var lastCellEndEditColumn = gridDetails.Columns[_lastCellEndEdit.ColumnIndex];
if (lastCellEndEditColumn.DisplayIndex >= GetVisibleColumnsMaxDisplayIndex())
{
iColNew = GetFirstVisibleColumnIndex();
iRowNew = gridDetails.CurrentCell.RowIndex;
}
else
{
iColNew = GetNextVisibleColumnIndex(lastCellEndEditColumn.DisplayIndex);
iRowNew = _lastCellEndEdit.RowIndex;
}
gridDetails.CurrentCell = gridDetails[iColNew, iRowNew];
}
}
_lastCellEndEdit = null;
}
private DataGridViewCell _lastCellEndEdit;
private void gridDetails_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
_lastCellEndEdit = gridDetails[e.ColumnIndex, e.RowIndex];
}
int GetVisibleColumnsMaxDisplayIndex()
{
int maxDisplayIndex = -1;
foreach (DataGridViewColumn column in gridDetails.Columns)
{
if (column.Visible && column.DisplayIndex > maxDisplayIndex)
{
maxDisplayIndex = column.DisplayIndex ;
}
}
return maxDisplayIndex;
}
int GetFirstVisibleColumnIndex()
{
var firstVisibleColumnDisplayIndex = gridDetails.Columns.Count - 1;
var firstVisibleColumnIndex = 0;
foreach (DataGridViewColumn column in gridDetails.Columns)
{
if (column.Visible && column.DisplayIndex < firstVisibleColumnDisplayIndex)
{
firstVisibleColumnDisplayIndex = column.DisplayIndex;
firstVisibleColumnIndex = column.Index;
}
}
return firstVisibleColumnIndex;
}
int GetNextVisibleColumnIndex(int previousColumnDisplayIndex)
{
var nextVisibleColumnDisplayIndex = gridDetails.Columns.Count;
var nextVisibleColumnIndex = previousColumnDisplayIndex;
foreach (DataGridViewColumn column in gridDetails.Columns)
{
if (column.Visible && column.DisplayIndex > previousColumnDisplayIndex
&& (column.DisplayIndex - previousColumnDisplayIndex) <= (nextVisibleColumnDisplayIndex - previousColumnDisplayIndex))
{
nextVisibleColumnDisplayIndex = column.DisplayIndex;
nextVisibleColumnIndex = column.Index;
}
}
return nextVisibleColumnIndex;
}