views:

55

answers:

2

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

  • Email Address
  • Resolved Email Address (calculated based on the input in Email Address)

After the user inputs or changes a value in Email Address, I want the value in Resolved Email Address to update based on a separate method. What event should I tie in to so that that the Resolved Email Address cell is updated after the Email Address cell is updated?

I'm not sure where to put the event because the user could do a number of things after adding/changing a value: tab to the next cell, click cancel on the form, click ok on the form, etc. Any ideas?

+1  A: 

CellValueChanged fires when the editing has finished, e.g. when the user navigated away from the cell.

Obalix
if the user clicks the "OK" or "Cancel" buttons, is there a way to stop the firing of those respective events if something is invalid in the "Cell Value Changed" event?
Ben McCormack
@Ben McCormack: see tsmatt's answer after the line.
Obalix
+1  A: 

Here's what I have done:

Use the dataGridView's CellBeginEdit and CellEndEdit events to control things. If you are editing the Email Address cell and tab out of it, it will fire the CellEndEdit event.

You should get a CellEndEdit event and a button click event when you click one of those buttons - in my tests, I always got the CellEndEdit first, but I suppose YMMV.

If there was something invalid when the CellEndEdit occurs, you could certain keep the OK/Cancel code from being executed.


Additionally, you could use the CellValidating event and e.Cancel the event if the value is invalid. That'll keep the button click event from firing and leave you in the edited cell.

itsmatt