如何在DataGridViewCheckboxCell上获取最后一个点击事件
我正在使用Winform面板中的DataGridView内部使用 dataGridViewCheckboxboxboxboxboxcolumn 。
单击复选框时,我需要计算可能在DataGridView之外更改控制状态的内容。
为此,我必须处理 cellcontentClick 事件,因为我只有在实际更改了复选框值时才能计算。
Grid.CellContentClick += Grid_CellContentClick
private void Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
dgv.EndEdit();
// Compute stuff
}
但是,CellContentClick并不总是会触发,而确实更改DataGridViewCheckBoxCell检查状态的内部事件是。
最重要的是,复选框上的快速连续点击请勿触发CellContentClick,仅在用户停止点击之前捕获了第一个单击。
结果,我最终处于无效状态,在该状态下,由于计算不使用复选框最终值,因此数据杂志外部的控件未按预期显示。
我已经尝试 debounce 活动并使用Mousedown和Grid RealOnlyly创建一个pseudo-lock财产,没有成功。
有没有办法只捕获一系列点击的最后一个事件?有更好的方法吗?
I'm using a DataGridViewCheckBoxColumn inside a DataGridView in a WinForm panel.
When a checkbox is clicked, I need to compute things that might change a Control state outside the DataGridView.
To do so, I have to handle the CellContentClick event because I need to compute only when a checkbox value is actually changed.
Grid.CellContentClick += Grid_CellContentClick
private void Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
dgv.EndEdit();
// Compute stuff
}
However CellContentClick doesn't always fire, while the internal event that does change the DataGridViewCheckboxCell checked state is.
Most importantly, fast successive clicks on a checkbox do not fire CellContentClick, only the first is catched until the user stops clicking.
As a result I end up in an invalid state where the control outside the DataGridView doesn't display as intended because the computation doesn't use the checkboxes final values.
I've tried to debounce the event and creating a pseudo-lock using MouseDown and the grid ReadOnly property, with no success.
Is there a way to catch only the last event of a series of clicks? Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢@jimi和@johng的见解,它帮助我解决了这个问题。
我无法使用
CellValueChanged
和CellContentClick
,即使使用Async和等待Task.delay(...)
,之后我的计算中正确并触发了线际上的异常。不过,这可能只是我,但是无论如何,我并不喜欢在这种情况下使用
螺纹
。我尚未考虑使用
cellValueChanged
,并且注意到它不会触发dataGridViewCheckBoxCell
时,我最终读取此线程实际上非常简单。每次单击网格复选框时都执行此代码,并且具有更好的逻辑,因为它依赖于直接值更改。
但是,这意味着每次更改值时进行计算。
我相信一个人可以 debounce 计算以改善该解决方案,幸运的是我资源昂贵,因此运行顺利,我不需要走那么远。
Thank you @Jimi and @JohnG for your insights, it helped me solving this issue.
I could not make it work using
CellValueChanged
andCellContentClick
, even with async andawait Task.Delay(...)
as it did not fire correctly and triggered inter-thread exceptions in my computation afterwards.It might just have been me though, but I wasn't very fond of using
Threading
in this context anyway.I hadn't considered using
CellValueChanged
and noticed that it wouldn't trigger for aDataGridViewCheckBoxCell
when clicked, so I ended up reading this thread and the solution is actually quite simple.This code is executed each time a grid checkbox is clicked on, and has a far better logic since it relies on a direct value change.
However it means the computation takes place each time the value is changed.
I believe one could debounce the computation in order to improve this solution, fortunately mine isn't too resource expensive so it runs smoothly and I don't need to take it that far.