DataGridView Events

I need to complete the task when the user usually ends the editing mode (regardless of whether the user really changed the value or not, but not when the user cancels the editing mode by pressing ESC) in the TextBox DataGridView control column.

I tried several events of the DataGridView element itself and the edit control, but none of them do exactly what I want:

DataGridView.CellValidating and DataGridView.CellValidated :

These events are fired every time the user selects another cell, even if the cell is not in edit mode. I tried to check the IsCurrentCellDirty property inside the CellValidating event. This is almost what I need, but IsCurrentCellDirty set only when the user really changes the value. But I also need to complete the task when the user usually ends the editing mode without changing anything. These events are not triggered when the user cancels the editing mode, which is good.

DataGridView.CellValueChanged

This event also fires too often (it also fires when a cell value is set programmatically).

DataGridView.CellEndEdit

This event is almost what I want. But it also starts when the user cancels the editing mode by pressing ESC. Is there any way to check if the editing mode has been edited inside the CellEndEdit event?

DataGridView.CellParsing

This event is almost what I want. But it does not start when the user ends the editing mode without changing anything.

Validating and Validated edit control events

I registered for these events inside the DataGridView.EditingControlShowing event. They do almost what I want, but they also work when the user cancels the editing mode by pressing ESC. Is there any way to check if the editing mode has been edited inside these events?

Any other suggestions for events that I could register and / or flags that I could check to achieve the desired behavior?

+4
source share
2 answers

What you can do is register in the PreviewKeyDown event of the EditingControl event inside the EditingControlShowing DataGridView event. From there, you can determine whether escape was pressed in the edit control and set a flag that will be considered as the CellEndEdit event.

You can derive the necessary events for registration from the names of the methods. This assumes that you have a bool field in your class called escapePressed , which (unsurprisingly) is the escape flag.

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.PreviewKeyDown -= Control_PreviewKeyDown; //avoid attaching multiple handlers in case control is cached e.Control.PreviewKeyDown += new PreviewKeyDownEventHandler(Control_PreviewKeyDown); } void Control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Escape) { Console.WriteLine("escape pressed"); escapePressed = true; } } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (!escapePressed) { Console.WriteLine("do your stuff"); //escape was not pressed. } else escapePressed = false; //reset the flag } 
+4
source

Here is my way:

Enter

 private DataGridViewCell cellBeingEdited = null; 

In DataGridView.EditingControlShowing

 cellBeingEdited = DataGridView.CurrentCell; 

In DataGridView.CellEndEdit

 cellBeingEdited = null; 

Then I can use the DataGridView.CellValidating event, which does not fire when editing is canceled, and check the cellBeingEdited field:

 if (DataGridView.CurrentCell != cellBeingEdited) return; 
+1
source

Source: https://habr.com/ru/post/1389999/


All Articles