我如何可以验证输入到一个DataGridView单元格的编辑控件? [英] How can I validate input to the edit control of a cell in a DataGridView?

查看:118
本文介绍了我如何可以验证输入到一个DataGridView单元格的编辑控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎捕获一个DataGridView控制的小区内的关键preSS事件,以验证他们键入用户输入的唯一方法,是使用在DataGridView控制OnEditControlShowing事件中,一种方法挂接到编辑控件的(e.Control)键preSS事件,并做一些验证。

It appears that the only way to capture the keypress events within a cell of a DataGridView control in order to validate user input as they type, is to use the DataGridView controls OnEditControlShowing event, hook up a method to the edit control's (e.Control) keypress event and do some validation.

我的问题是,我已经建立了自定义的DataGridView列类的一堆,用自己的自定义单元格类型。这些细胞有自己的自定义编辑控件(像DateTimePickers和数字或货币文本框。)

My problem is that I've built a heap of custom DataGridView column classes, with their own custom cell types. These cells have their own custom edit controls (things like DateTimePickers and Numeric or Currency textboxes.)

我想要做一些数字验证这些细胞具有货币文本框的数字作为自己的编辑控件,但不是所有的其他类型的细胞。

I want to do some numeric validation for those cells that have Numeric of Currency Textboxes as their edit controls but not all the other cell types.

我怎么能确定,在DataGridView中的OnEditControlShowing覆盖,特定的编辑控件是否需要一些数字验证?

How can I determine, within the DataGridView's "OnEditControlShowing" override, whether or not a particular edit control needs some numeric validation?

推荐答案

如果我正确地理解你的问题,你要选择要连接的基础上的类型的编辑控制的的事件。如果是这样,这是我会怎么做:

If I understand your question correctly, you want to choose to wire up an event based on the type of the editing control. If so, this is what I'd do:

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        //Remove any KeyPress events already attached
        e.Control.KeyPress -= new KeyPressEventHandler(FirstEditingControl_KeyPress);
        e.Control.KeyPress -= new KeyPressEventHandler(SecondEditingControl_KeyPress);

        //Choose event to wire based on control type
        if (e.Control is NumericTextBox)
        {
            e.Control.KeyPress += new KeyPressEventHandler(FirstEditingControl_KeyPress);
        } else if (e.Control is CurrencyTextBox)
        {
            e.Control.KeyPress += new KeyPressEventHandler(SecondEditingControl_KeyPress);
        }
    }

我从经验中学到任何无线化可能发生的事件的编辑控件的DataGridView的,因为它们会重复使用相同的控制,多个单元格。

I've learned from experience to unwire any possible events on editing controls in DataGridView's, since they will reuse the same control for multiple cells.

这篇关于我如何可以验证输入到一个DataGridView单元格的编辑控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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