如何在编辑DataGridViewTextBoxColumn并按EnterKey后阻止进入下一行?

Max*_*Max 12 c# datagridview focus keypress datagridviewtextboxcell

我正在研究一个程序DataGridViews.在一个中DatagridView有一个DataGridViewTextBoxColumn,它可以由用户编辑.当用户完成键入数字时,他按下键盘上的ENTER.现在它DataGridView做了所有的Events,毕竟Events,最后一件事就是问题.

一切都完成了,Windows将选择下一个DataGridViewRow,我无法阻止这一点.

我试过了

if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true; // or e.Handled 
Run Code Online (Sandbox Code Playgroud)

在我发现的几乎所有事件中.可悲的是,我只能在DataGridViewTextBoxColumn未处于编辑模式时阻止ENTER键.

继续我的方法在编辑时找到了ENTER

添加事件

private void dgr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress += new KeyPressEventHandler(dgr_KeyPress_NumericTester);
}
Run Code Online (Sandbox Code Playgroud)

这是仅接受数字输入的事件.

private void dgr_KeyPress_NumericTester(object sender, KeyPressEventArgs e)
{
    if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8) e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)

详细解释:

当用户输入一个具有某些依赖性的Value时,我想给另一个控件提供焦点,因此他用来纠正依赖.

我也尝试过,DependingControl.Focus()但最后一次"输入"将是视图中的最后一件事.

有人知道如何防止这种情况吗?

V4V*_*tta 9

我尝试通过从Textbox列继承customcolumn并覆盖下面的事件来更改Grid的Enter行为

protected override bool ProcessDialogKey(Keys keyData)
{
    if (keyData == Keys.Enter)
       return base.ProcessDialogKey(Keys.Tab);
    else
       return base.ProcessDialogKey(keyData);
}
Run Code Online (Sandbox Code Playgroud)

因此,不是发送Enter键而是模拟Tab的动作,它将移动到下一个单元格.希望这可以帮助


Dav*_*all 6

好吧,我设法得到了一些可以做你想要的东西(或者至少做了很难的部分,我认为你已经完成了大部分其他工作)但是解决方案让我的皮肤爬行.

我最后在编辑单元格时"取消"输入键事件以使用CellEndEdit事件和SelectionChanged事件的混合.

我介绍了一些存储某些状态的类级别字段 - 特别是在编辑单元格结束时我们所处的行以及我们是否停止更改选择.

代码如下所示:

public partial class Form1 : Form
{
    private int currentRow;
    private bool resetRow = false;

    public Form1()
    {
        InitializeComponent();

        // deleted out all the binding code of the grid to focus on the interesting stuff

        dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

        // Use the DataBindingComplete event to attack the SelectionChanged, 
        // avoiding infinite loops and other nastiness.
        dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
    }

    void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if (resetRow)
        {
            resetRow = false;
            dataGridView1.CurrentCell = dataGridView1.Rows[currentRow].Cells[0];          
        }
    }

    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        resetRow = true;
        currentRow = e.RowIndex;
    }

    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
    }
} 
Run Code Online (Sandbox Code Playgroud)

你需要彻底测试它,以确保它完全符合你的需要.我只是检查它是否在按下编辑控件输出时停止了行更改.

就像我说的那样 - 我不太高兴需要做这样的事情 - 它感觉很脆弱,而且它可能会产生奇怪的副作用.但是,如果你必须有这种行为,并且你测试得好,我认为这是你想要的唯一方法.


小智 6

Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyData = Keys.Enter Then e.Handled = True
End Sub
Run Code Online (Sandbox Code Playgroud)

这只是一种解决方法,不是真正的解决方案,但它确实有效。