After what event should I perform an action after editing a cell in a DataGridView?

I have a DataGridView WinForms control in the form. There are two fields in this DataGridControl:

  • Email Address
  • Resolved Email Address (calculated based on input to Email Address )

After the user enters or changes the value in the Email Address , I want the value in the Resolved Email Address updated based on a separate method. What event should I bind to update the Resolved Email Address cell after updating the Email Address cell?

Iโ€™m not sure where to place the event, because the user could do several things after adding / changing the value: tab to the next cell, click โ€œCancelโ€ in the form, click โ€œOKโ€ in the form, etc. ideas?

+4
source share
3 answers

Here is what I did:

Use the dataGridView events CellBeginEdit and CellEndEdit to manage things. If you edit and exit an email address cell, it fires the CellEndEdit event.

You should receive a CellEndEdit event and a button click event when one of these buttons is clicked. In my tests, I always got CellEndEdit, but I suppose YMMV.

If CellEndEdit failed, you might not correctly execute the OK / Cancel code.


Alternatively, you can use the CellValidating event and the e.Cancel event if the value is invalid. This will disable the button click event and leave you in an edited cell.

+1
source

CellValueChanged triggered when editing is completed, for example. when the user moves away from the cell.

+1
source

The order of events related to what you are trying to do is:

  • 1) CellValidating

  • 2) CellValueChanged

  • 3) CellValidated

  • 4) CellEndEdit

I would recommend using CellValidating if you want to prevent further actions based on the contents of the cell, because in CellValidating you can set the e.Cancel flag contained in the DataGridViewCellValidatingEventArgs to true, which will automatically return control to this cell.

If you just want to refresh the display (resolved email name), use CellValueChanged .

 private void myDataGridView_CellValueChanged (object sender, DataGridViewCellEventArgs e)
 {
     e.Cancel = (! validateMyInput (e.FormattedValue.ToString ());
 }
+1
source

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


All Articles