CellValueChanged vs CellValidating Events for DataGridView

17,868

I'm using CellValueChanged currently on a grid with custom validation and have had no problems with display or anything else.

I used this event because I wanted to fire off a certain order of events, but only when the user changes the value of a cell.

I have not noticed much in the way of a performance hit (tested with 100 - 5000 rows).

I think in the end it depends on what your validation needs are. In my case, CellValueChanged has done what I wanted/needed.

EDIT

The biggest thing about the CellValidating event is that you can stop the user from leaving a cell, if the value entered does not pass your validation. I didn't want to do this.

Share:
17,868
Kharlos Dominguez
Author by

Kharlos Dominguez

Updated on September 25, 2022

Comments

  • Kharlos Dominguez
    Kharlos Dominguez over 1 year

    What's the best place to implement validation logic code and conditional formatting code for a DataGridView?

    In a lot of books and articles that I've read on this control, it seems to suggest that the appropriate event to handle for this is the CellValidating one. Well, the name more than implies this as well.

    However, this event triggers a bit too often for my tastes and I'm not sure it is required. For example, this event triggers everytimes the users switches to another row.

    On the other hand, the CellValueChanged event seems to trigger only when the value of the cell changes, which means the validation code runs only when the value changes and not everytime a user changes cells.

    Now, since so many books use the CellValidating event, I wonder if there is not any gotcha (in display for example) with using the CellValueChanged?

    I understand that the impact in performance should be irrelevant when using simple validation and conditional highlighting rules but I would as much prefer it not to run useless code everytime the user moves to another cell if it can be avoided.

    Thanks,

  • Kharlos Dominguez
    Kharlos Dominguez over 13 years
    Thanks, good point about CellValidating allowing to stop the user from leaving the cell. Not something I need in my current project but I don't think I remembered this difference.
  • ss7
    ss7 almost 9 years
    I don't want to stop the user leaving the cell if it doesnt pass validation. Can you give me an example of your CellValueChanged function? Right now I'm using CellValidating, CellValidated, and CellEndEdit. But I think your method is the best. If you can provide and example it would help a lot.
  • miki
    miki about 8 years
    "CellValidating event is that you can stop the user from leaving a cell" which also prevents user from closing the Form. Not good practice, but can be useful in particular situation.