关于DataGridView控件的事件控件、事件、DataGridView

2023-09-04 04:43:53 作者:花祭°

我制定了datagridview的过滤应用程序。我用DataGridView中的 dataGridView1_CellValueChanged(对象发件人,DataGridViewCellEventArgs E) 事件过滤。 但我想处理它的关键preSS事件DataGridView的单元格。但我没有得到这类型的事件。

I'm developed an application for the datagridview filtering . And i used the datagridview's dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) event for filtering. But i want to handle it on the key press event for the datagridview cell. But i'm not getting that type of event.

在DataGridView事件应该occure   对每个键preSS ..

the datagridview event should occure on the each keypress..

所以,有谁能够告诉我,哪个事件我应该使用在DataGridView?

So can anybody tell me that which event should i use for the datagridview?

请帮我... 感谢名单

please help me... thanx

推荐答案

DataGridView.Key preSS 事件将不可以提高当在一个特定的小区内的用户的类型。如果你想在每次preSS,而编辑在单元格中的内容的一个关键时间得到通​​知,你有两个选择:

The DataGridView.KeyPress event will not be raised when the user types in a particular cell. If you want to be notified each time they press a key while editing content in a cell, you have two options:

办理的是直接提出的编辑控件本身(你可以使用快捷键preSS 事件 EditingControlShowing 事件)。

例如,可以使用下面的code:

For example, you might use the following code:

public class Form1 : Form
{
    public Form1()
    {
        // Add a handler for the EditingControlShowing event
        myDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(myDGV_EditingControlShowing);
    }

    private void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        // Ensure that the editing control is a TextBox
        TextBox txt = e.Control as TextBox;
        if (txt != null)
        {
            // Remove an existing event handler, if present, to avoid adding
            // multiple handler when the editing control is reused
            txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);

            // Add a handler for the TextBox's KeyPress event
            txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
        }
    }

    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        // Write your validation code here
        // ...

        MessageBox.Show(e.KeyChar.ToString());

    }
}

创建继承了标准的DataGridView 控件的自定义类并覆盖其的 ProcessDialogKey 方法。这种方法被设计为处理每个键事件,甚至没有 这发生在编辑控制。您可以处理的关键presses的覆盖方法里面,或提高自己的事件,您可以附加一个单独的处理方法。

Create a custom class that inherits from the standard DataGridView control and override its ProcessDialogKey method. This method is designed to process each key event, even those that occur on the editing control. You can either handle the key presses inside of that overridden method, or raise an event of your own to which you can attach a separate handler method.

 
精彩推荐
图片推荐