DataGridView的CellValueChanged vs CellValidating事件 [英] CellValueChanged vs CellValidating Events for DataGridView

查看:147
本文介绍了DataGridView的CellValueChanged vs CellValidating事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为DataGridView实现验证逻辑代码和条件格式代码的最佳位置是什么?

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

在我已经阅读过很多关于此控件的书籍和文章中,似乎暗示处理此事件的适当事件是CellValidating事件。
好​​吧,这个名字不仅暗示了这一点。

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.

另一方面,CellValueChanged事件似乎仅在以下情况下触发:单元格更改,这意味着验证代码仅在值更改时才运行,而不是在用户每次更改单元格时才运行。

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.

现在,由于有很多书籍使用CellValidating事件,我想知道如果没有使用CellValueChanged(例如在显示中)的陷阱,

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.

谢谢,

推荐答案

我目前正在使用带有自定义验证的网格上的CellValueChanged,并且在显示或其他方面没有任何问题。

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

我使用此事件是因为我想解雇ff确定事件的顺序,但仅当用户更改单元格的值时。

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.

我对性能影响的方式并没有多大注意(测试100 -5000行)。

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

最后,我认为这取决于您的验证需求。就我而言,CellValueChanged已经完成了我想要/需要的操作。

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

编辑

CellValidating事件的最大优点是,如果输入的值未通过您的验证,则可以阻止用户离开单元格。我不想这样做。

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.

这篇关于DataGridView的CellValueChanged vs CellValidating事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆