防止按Enter键将Windows Forms DataGridView移至下一行 [英] Preventing Windows Forms DataGridView moving to next row on pressing ENTER key

查看:110
本文介绍了防止按Enter键将Windows Forms DataGridView移至下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题(或它的变体)出现了几次。但是到目前为止,我还没有找到适合我的解决方案。

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.

我正在使用C#编写一个Windows Forms UserControl,其中包含一个DataGridView来呈现一个只读集合。员工数据作为一种荣耀的选择列表。网格是只读的(在control_load上填充),并且已将FullRowSelect设置为选择方法。我希望用户既可以双击鼠标,也可以使用当前行上的Enter键选择一个Id值形式,该行将由订阅者提取并在其他地方处理。

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. The grid is read-only (populated on control_load) and has FullRowSelect set as the selection method. 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.

在分配我选择的雇员值后处理KeyDown事件时,我试图防止选择移到下一行。当CurrentCell.RowIndex为零时,例外可以正常工作。
有人知道我如何才能为CurrentCell.Rowindex = 0吗?

In handling the KeyDown event after assigning my selected employee value, I attempt to prevent selection moving to the next row. This works fine except when the CurrentCell.RowIndex is zero. Does anyone know how I can get this working for 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抬起头来。我所需要做的就是设置 e.Handled = true e.SuppressKeyPress = true 替换 if(dgvEmployees.CurrentCell.RowIndex> 0)语句完整。

Thanks to Reniuz fo the heads up. 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.

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;
}

这篇关于防止按Enter键将Windows Forms DataGridView移至下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆