Event similar to textchanged of textbox inside datagridview

ankit goel 766 Reputation points
2023-07-12T15:03:46.1833333+00:00

I have a datagridview in which I am looking for a event to capture in which, when the values of a cell is modifying by the user (not modified or editing has been done) , should works Just like textchanged event for a textbox .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-07-14T14:19:14.49+00:00

    There are many events to handle or methods to override when something is changing or changed in DataGridView, and the right answer really depens on the your actual requirements.

    • Do you want to customize the Enter key? Then you may want to override ProcessDialogKey.
    • Do you want to customize the arrow keys? Then you may want to override ProcessDataGridViewKey
    • Do you want to handle TextChanged event of the text box editing control? Then continue reading the answer!
    • Do you want ...? Then let me know what exactly is your requirement and I'll share some idea.

    Speaking of TextChanged event of TextBox, I guess you are interested in handling same exact event of the DataGridView while editing a text in text box of a cell. There's an event EditingControlShowing, that raises right before editing the cell when the editing control is gonna display. You can get the editing control and check if it's a TextBox (it's actually a DataGridViewTextBoxEditingControl which is derived from TextBox), then just handle its TextChanged event. For example:

    private dgv1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        var txt = e.Control as TextBox;
        if(txt!=null)
        {
            txt.TextChanged -= txt_TextChanged;
            txt.TextChanged += txt_TextChanged;
        } 
    }
    private txt_TextChanged(object sender, EventArgs e)
    {
        //Do whatever you want. 
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-07-13T02:00:24.7933333+00:00

    Hi @ankit goel , Welcome to Microsoft Q&A.

    In fact, as far as I know there is no such event.

    First when we enter a datagridviewtexbox and input characters: only happens

    1. Enter the control event:
    • Enter Event: Fired when the DataGridViewTextBox gets focus and becomes the active control.
    • GotFocus event: Fired when the DataGridViewTextBox gets focus.
    1. Keyboard input event:
    • PreviewKeyDown** event: Triggered before pressing a keyboard key, it can detect whether a special key (such as arrow keys, enter key) is pressed.
    • KeyDown event: Triggered when a keyboard key is pressed, at this time, the information of the keyboard key can be obtained.
    • KeyPress event: Triggered when the keyboard key representing the character is pressed, at this time, the information of the character input can be obtained.

    I can think of two alternatives:

    • One is the one I gave you earlier: do a commit on the enter press event and get back to editing.
    • Another way: judge directly in the keydown event, if a cell is in the editing state, perform a certain operation. (You may need to do too much judgment with this method; you may need to do judgment for most keys.)

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.