如何prevent编辑DataGridViewTextBoxColumn和pressing EnterKey后去下一行? [英] How to prevent going to next row after editing a DataGridViewTextBoxColumn and pressing EnterKey?

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

问题描述

我工作的一个程序 DataGridViews
在一个 DataGridView的有一个 DataGridViewTextBoxColumn ,它允许由用户进行编辑。当用户用输入数字到它完成的,他presses键盘上的ENTER。现在 DataGridView的执行其所有的活动和所有的活动后的最后一件事就是问题所在。

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.

一切都做,和Windows是要选择下一个的DataGridViewRow ,我无法prevent这一点。

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

我试过

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

在几乎每一个事件,我发现。可悲的是我只能以prevent ENTER键时, DataGridViewTextBoxColumn 是不是在编辑模式。

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

添加事件。

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

要详细解释:

当用户输入一个数值,它有一些dependings,我想给另一个控制的重点,所以他是用来纠正dependings。

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.

我也试了一下 DependingControl.Focus(),但最后的输入将是在视图上的最后一件事。

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

有人不知道如何prevent呢?

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.

我最终什么了以取消编辑单元格时,作为使用和 CellEndEdit 事件的混合物进入关键事件的的SelectionChanged 事件。

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.

在code是这样的:

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

您会希望彻底测试以确保它不正是你所需要的。我只检查,看,它并停止行更改时pressing进入了编辑控制的。

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.

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

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