簡體   English   中英

防止按Enter鍵將Windows Forms DataGridView移至下一行

[英]Preventing Windows Forms DataGridView moving to next row on pressing ENTER key

我知道這個問題(或它的變體)出現了幾次。 但是到目前為止,我還沒有找到適合我的解決方案。

我正在使用C#編寫一個Windows Forms UserControl,其中包含一個DataGridView,以呈現員工數據的只讀集合作為一種美化的選擇列表。 網格是只讀的(在control_load上填充),並且已將FullRowSelect設置為選擇方法。 我希望用戶既可以雙擊鼠標,也可以使用當前行上的Enter鍵從該行中選擇一個Id值,該行將由訂閱者獲取以在其他地方處理。

在分配我選擇的雇員值后處理KeyDown事件時,我試圖防止選擇移到下一行。 除非 CurrentCell.RowIndex為零, 否則此方法工作正常。 有誰知道我如何才能讓CurrentCell.Rowindex = 0起作用呢?

private void dgvEmployees_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (dgvEmployees.CurrentRow.Cells[0].Value != null)
        {
            this.SelectedEmployeeId = (int) dgvEmployees.CurrentRow.Cells[0].Value;
            this.OnEmployeeSelected(new TestEmployeeGridListEventArgs() { 
            SelectedEmployeeId = this.SelectedEmployeeId, 
            SelectedEmployeeIdentifier = dgvEmployees.CurrentRow.Cells["Identifier"].Value.ToString() 
            });
        }

        // Prevent pressing <enter key> moving onto the next row.
        if (dgvEmployees.CurrentCell.RowIndex > 0)
        { 
            dgvEmployees.CurrentCell = dgvEmployees[1, dgvEmployees.CurrentCell.RowIndex - 1];
            dgvEmployees.CurrentRow.Selected = true;
        } 
        else
        {
            dgvEmployees.CurrentCell = dgvEmployees[1, 0];
            dgvEmployees.Rows[0].Cells[1].Selected = true;
        }           
    }
}

感謝Reniuz fo抬起頭來。 我只需要設置e.Handled = truee.SuppressKeyPress = true替換if (dgvEmployees.CurrentCell.RowIndex > 0)語句。

if (e.KeyCode == Keys.Enter)
{
    if (dgvEmployees.CurrentRow.Cells[0].Value != null)
    {
        this.SelectedEmployeeId = (int) dgvEmployees.CurrentRow.Cells[0].Value;
        this.OnEmployeeSelected(new TestEmployeeGridListEventArgs() { 
            SelectedEmployeeId = this.SelectedEmployeeId, 
            SelectedEmployeeIdentifier = dgvEmployees.CurrentRow.Cells["Identifier"].Value.ToString() 
        });
    }

    e.SuppressKeyPress = true;
}

只需嘗試一下...。它完全可以在DatagridView CellEdit Event中工作,然后輸入然后將焦點放在下一個單元格而不是行上。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

 {

 // Check if Enter is pressed //  DGV Cell Edit // dgv1 as DataGrideView

 if (keyData == Keys.Enter /* && txtledger.Text != "" */)
 {

 try       
{

   if (dgv1.CurrentCell.ColumnIndex == 18 ) 

// 18 is Column Count and focusing length

       {                       

        dgv1.CurrentCell = dgv1.Rows[dgv1.CurrentRow.Index + 1 ].Cells[1];
        return true;
       }
       else
       {
        SendKeys.Send("{Right}");  //Tab OR Right Key Ur Need
       }
     }
  catch (Exception e)

  {                    

  dgv1.Rows.Add();
  dgv1.CurrentCell = dgv1.Rows[dgv1.CurrentRow.Index].Cells[1];

  }

  return true;

  }

  return base.ProcessCmdKey(ref msg, keyData);

  }

這將正常工作..它禁用了datagridview內部正常的回車鍵處理工作單元格編輯事件,以使同一行中的下一個單元格集中。 如果有任何問題,請以您的形式檢查所有keydown事件...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM