编辑 DataGridViewTextBoxColumn 并按 EnterKey 后,如何防止转到下一行? [英] How to prevent going to next row after editing a DataGridViewTextBoxColumn and pressing EnterKey?

查看:30
本文介绍了编辑 DataGridViewTextBoxColumn 并按 EnterKey 后,如何防止转到下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataGridViews 开发一个程序.在一个 DatagridView 中有一个 DataGridViewTextBoxColumn,用户可以对其进行编辑.当用户完成将数字输入其中时,他按键盘上的 ENTER.现在DataGridView 完成它所有的Events,毕竟Events,最后一件事就是问题.

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

我试过了

if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true;//或 e.Handled

几乎在我发现的每个事件中.遗憾的是,当 DataGridViewTextBoxColumn 未处于编辑模式时,我只能阻止 ENTER 键.

这是我在编辑时找到 ENTER 的方法

添加事件

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

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

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

详细说明:

当用户输入一个值时,有一些依赖,我想给另一个控件焦点,所以他用来纠正依赖.

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

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

解决方案

好吧,我设法让一些工作可以满足您的需求(或者至少完成了困难的部分,我认为您已经完成了大部分其他工作)但解决方案让我的皮肤爬行.

我最终在编辑单元格时取消"了回车键事件,以混合使用 CellEndEdit 事件和 SelectionChanged 事件.>

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

代码如下:

公共部分类 Form1 : Form{私人 int currentRow;private bool resetRow = false;公共 Form1(){初始化组件();//删除网格的所有绑定代码以专注于有趣的东西dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);//使用DataBindingComplete事件攻击SelectionChanged,//避免无限循环和其他讨厌的东西.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);}void dataGridView1_SelectionChanged(对象发送者,EventArgs e){如果(重置行){重置行 = 假;dataGridView1.CurrentCell = dataGridView1.Rows[currentRow].Cells[0];}}void dataGridView1_CellEndEdit(对象发送者,DataGridViewCellEventArgs e){重置行 = 真;currentRow = e.RowIndex;}void dataGridView1_DataBindingComplete(对象发送者,DataGridViewBindingCompleteEventArgs e){dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);}}

您需要对此进行彻底测试,以确保它完全符合您的需求.我只是检查了一下,当按 Enter 键退出编辑控件时,它确实会停止行更改.

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

I'm working on a program with DataGridViews. In one DatagridView there is a DataGridViewTextBoxColumn, which is enabled to be edited by the user. When the user is done with typing the numbers into it, he presses ENTER on the keyboard. Now the DataGridView does all its Events, and after all Events, the last thing is the problem.

Everything is done, and Windows is going to Select the next DataGridViewRow, and I'm not able to prevent this.

I tried

if (e.KeyData == Keys.Enter) e.SuppressKeyPress = true; // or e.Handled 

in nearly every event I found. Sadly I was only able to Prevent the ENTER key when the DataGridViewTextBoxColumn is not in edit mode.

Heres my methode t find the ENTER while in Editing

Adding the Event

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

And this is the event to accept numeric input only.

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

To explain in detail:

When the user enters a Value, that has some dependings, I would like to give another control the focus, so he is used to correct the dependings.

I also tried it with DependingControl.Focus() but the last "enter" is going to be the last thing on the view.

Does someone know how to prevent this?

解决方案

Well, I managed to get something working that does what you want (or at least does the hard part, I think you have already done most of the other stuff) but the solution makes my skin crawl.

What I ended up with to "cancel" the enter key event when editing a cell as to use a mixture of the CellEndEdit event and the SelectionChanged event.

I introduced a couple of class level fields that store some state - in particular what row we are in at the end of editing a cell and whether we are stopping a selection changed.

The code looks like this:

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

You'll want to test this thoroughly to make sure it does exactly what you need. I only checked to see that it does stop a row change when pressing enter out of an editing control.

As I said - I'm not too happy with needing to do something like this - it feels quite brittle, and also like it could have weird side effects. But if you must have this behaviour, and you test it well I think this is the only way to do what you want.

这篇关于编辑 DataGridViewTextBoxColumn 并按 EnterKey 后,如何防止转到下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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