开发者

About datagridview control's event

开发者 https://www.devze.com 2023-02-13 08:23 出处:网络
I\'m developed an application for the datagridview filtering . And i used the datagridview\'sdataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

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.

the datagridview event should occure on the each keypress..

So can anybody tell me that which event should i use for the dat开发者_如何转开发agridview?

please help me... thanx


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:

  1. Handle the KeyPress event that is raised directly by the editing control itself (which you can access using the EditingControlShowing event).

    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());
    
        }
    }
    
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号