views:

61

answers:

1

Hello,

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,

+1  A: 

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.

Tony Abrams
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.
Kharlos Dominguez