简体   繁体   English

防止按Enter键将Windows Forms DataGridView移至下一行

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

I know this question (or variants of it) has come up a few times. 我知道这个问题(或它的变体)出现了几次。 But So far I have not found a solution that works for me. 但是到目前为止,我还没有找到适合我的解决方案。

I'm writing a Windows Forms UserControl using C# containing a DataGridView to present a read-only collection of employee data as a kind of glorified select list. 我正在使用C#编写一个Windows Forms UserControl,其中包含一个DataGridView,以呈现员工数据的只读集合作为一种美化的选择列表。 The grid is read-only (populated on control_load) and has FullRowSelect set as the selection method. 网格是只读的(在control_load上填充),并且已将FullRowSelect设置为选择方法。 I want the user to be able either double mouse click or use the Enter Key on the current row to select an Id value form that row which gets picked up by subscribers to handle elsewhere. 我希望用户既可以双击鼠标,也可以使用当前行上的Enter键从该行中选择一个Id值,该行将由订阅者获取以在其他地方处理。

In handling the KeyDown event after assigning my selected employee value, I attempt to prevent selection moving to the next row. 在分配我选择的雇员值后处理KeyDown事件时,我试图防止选择移到下一行。 This works fine except when the CurrentCell.RowIndex is zero. 除非 CurrentCell.RowIndex为零, 否则此方法工作正常。 Does anyone know how I can get this working for CurrentCell.Rowindex = 0 ? 有谁知道我如何才能让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;
        }           
    }
}

Thanks to Reniuz fo the heads up. 感谢Reniuz fo抬起头来。 All I needed was either to set e.Handled = true or e.SuppressKeyPress = true replacing the if (dgvEmployees.CurrentCell.RowIndex > 0) statement in it's entirety. 我只需要设置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;
}

Just Try This....Its Perfectly Working In DatagridView CellEdit Event to enter then focus next cell not row. 只需尝试一下...。它完全可以在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);

  }

This will be working fine.. it is disable the normal enter key process inside the datagridview to work cell edit event to focus next cell in same row. 这将正常工作..它禁用了datagridview内部正常的回车键处理工作单元格编辑事件,以使同一行中的下一个单元格集中。 If any problem check all keydown event in ur form... 如果有任何问题,请以您的形式检查所有keydown事件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM