Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CellValueChanged vs CellValidating Events for DataGridView

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,

like image 411
Kharlos Dominguez Avatar asked Aug 12 '10 15:08

Kharlos Dominguez


1 Answers

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.

like image 109
Tony Abrams Avatar answered Sep 22 '22 18:09

Tony Abrams