[英]Handling Enter key in DataGridView
我有一個有5列的datagridview。 datagridview中的列是[項目代碼,描述,數量,價格和總計]。
它應該如何工作:當用戶輸入項目代碼時,應從數據庫中獲取描述和價格,並在各自的字段中顯示結果。 然后用戶輸入數量。
我想要什么:我想當用戶在第1欄(物品代碼)中輸入值時,下一個要選擇的單元格應該是第3列(數量)。 然后,如果輸入數量並且用戶按下回車按鈕,則選擇的下一個單元格應該是下一行的第一列(項目代碼)。
問題:我嘗試了幾個代碼,但它不起作用。 問題是當在第1列(項目代碼)中輸入項目代碼時,下一個單元格的位置是第 3列的第2行 (數量)。 它應該移動到同一行的第 3列(Quantity), 即第1行 。
以下是我使用過的代碼:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
currentRow = dataGridView1.CurrentCell.RowIndex;
currentColumn = dataGridView1.CurrentCell.ColumnIndex;
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (currentColumn == dataGridView1.Columns.Count - 5)
{
dataGridView1.CurrentCell = dataGridView1[currentColumn + 2, currentRow];
}
else
{
dataGridView1.CurrentCell = dataGridView1[currentColumn - 2, currentRow + 1];
}
}
感謝這篇文章。
我編輯了一下,這是我的代碼工作。
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
icolumn = dataGridView1.CurrentCell.ColumnIndex;
irow = dataGridView1.CurrentCell.RowIndex;
int i = irow;
if (keyData == Keys.Enter)
{
if (icolumn == dataGridView1.Columns.Count - 5)
{
dataGridView1.CurrentCell = dataGridView1[icolumn + 2, irow];
}
else
{
dataGridView1.CurrentCell = dataGridView1[icolumn - 2, irow + 1];
}
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.