[英]Button Click not firing When focus is set to next cell in DataGridView on CellEndEdit
在Windows窗體上,我有一個包含3列和幾個按鈕來處理數據處理的gridview。 網格是可編輯的,每當用戶在編輯模式下按“ Enter”鍵時,我就使用以下代碼將焦點移至當前行的下一個單元格。
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != dataGridView.Columns.Count - 1)
{
this.BeginInvoke(new MethodInvoker(() =>
{
dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1];
}));
}
}
該代碼工作正常,並根據需要將焦點移到下一個單元格。 但是,當我在網格單元格處於焦點狀態時單擊任何按鈕時,就會出現問題。 由於焦點位於某個單元格上,因此單擊按鈕會在按鈕的Click事件之前觸發CellEndEdit事件,並且由於我的代碼,焦點移到了下一個單元格,而按鈕單擊根本沒有觸發。 我要確保在單擊按鈕時不會觸發為移動到CellEndEdit函數中的下一個單元而編寫的代碼。
關於此問題,我在SO和Internet上進行了大量搜索,但找不到永久性的解決方案。 任何幫助將不勝感激。
您可以改用KeyDown
事件。
可能是這樣的:
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
// the rest of your code
e.Handled = true;
}
}
覆蓋form
ProcessCmdKey
,檢查CurrentCell
是否處於編輯模式,然后單擊Enter
:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && this.dataGridView.IsCurrentCellInEditMode)
{
if (this.dataGridView.CurrentCell.ColumnIndex != this.dataGridView.Columns.Count - 1)
{
//this.BeginInvoke(new MethodInvoker(() =>
//{
this.dataGridView.CurrentCell = this.dataGridView.CurrentRow.Cells[this.dataGridView.CurrentCell.ColumnIndex + 1];
//}));
}
return true;
}
else return base.ProcessCmdKey(ref msg, keyData);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.