仅当DataGridView中的Cell ValueChanged时如何运行CellEndEdit [英] How to run Run CellEndEdit only when Cell ValueChanged in DataGridView

查看:21
本文介绍了仅当DataGridView中的Cell ValueChanged时如何运行CellEndEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在单元格的值改变时运行 CellEndEdit,尝试把

i want to run CellEndEdit only when value of cell is changed, tried putting

if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == e.FormattedValue.ToString()) 
             return; 

在 CellValidation 事件中,Cell Validation 事件确实返回,但 CellEndEdit 也被执行和更新,更新日期 &by 字段,当用户仅进入编辑模式并在未更改值单元格的情况下出来时.
到达 CellEndEdit 时 CellValue &Formatted Value 相同,因此无法将其放入 CellEndEdit.

in CellValidation event, the Cell Validation event does return but CellEndEdit also gets executed and updates, updated date & by fields when the user has only gone into edit mode and came out without changing the value cell.
By the time CellEndEdit is reached CellValue & Formatted Value are same so couldn't put this in CellEndEdit.

一个简单的解决方案是在 CellValidation 中设置一个标志并在设置标志时返回 CellEndEdit,但这似乎是一个容易出错的解决方案,因为表单上大约有 10 个网格.那么 10 个标志?

A trivial solution is to set a flag in CellValidation and return CellEndEdit when flag is set, but this appears to be a error-prone solution as there are about 10 girds on the form. So 10 flags?

推荐答案

不要在 CellEndEdit 中执行您的任务,而是将它们放在 CellValueChanged 中.仅当单元格值更改时才会触发.请注意,它会在您的 DataGridViews 最初填充时触发,但要处理该问题,您可以只放置一个变量表示 formInitialized 或其他内容,以确保您在填充数据网格时没有执行 CellEndEdit.

Instead of performing your tasks in CellEndEdit, put them in CellValueChanged. It is fired only when cell value is changed. Please note that it will fire when your DataGridViews are initially populated, but to handle that, you can put just one variable saying formInitialized or something, to make sure that you are not executing your CellEndEdit when you populate your data grids.

为了回答您的问题,无法确定在触发 CellEndEdit 时值是否更改,因为它总是在单元格退出编辑模式时触发.唯一的解决方案是,就像您提出的那样,在外部存储旧值,但您已经注意到为什么那么糟糕(尽管在大多数情况下它确实很好用).

And to answer your question, there is no way to figure out if value is changed when CellEndEdit is fired, because it is always fired when cell gets out of edit mode. The only solution is, like you proposed, to store the old value externally, but you already noticed why is that bad (though it works really good in most of the cases).

这篇关于仅当DataGridView中的Cell ValueChanged时如何运行CellEndEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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