如何检测DataGridView CheckBox事件更改? [英] How to detect DataGridView CheckBox event change?

查看:50
本文介绍了如何检测DataGridView CheckBox事件更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winforms应用程序,并且希望在选中/取消选中 DataGridView 控件中嵌入的复选框时触发一些代码。我尝试过的每一个事件

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either


  1. 点击 CheckBox 都会触发,但是在其检查状态更改之前,或

  2. 仅在 CheckBox 失去焦点之后才触发

  1. Triggers as soon as the CheckBox is clicked but before its checked state changes, or
  2. Triggers only once the CheckBox looses its focus

我似乎找不到在检查状态更改后立即触发的事件。

I can't seem to find event that triggers immediately after the checked state changes.

编辑:

我要实现的是,当 CheckBox的选中状态时在一个 DataGridView 中进行了更改,其他两个 DataGridView 的数据进行了更改。但是我使用过的所有事件,其他网格中的数据仅在第一个 DataGridView CheckBox 丢失后才发生变化焦点。

What I am trying to achieve is that when the checked state of a CheckBox in one DataGridView changes, the data in two other DataGridViews changes. Yet all the events I have used, the data in the other grids only changes after the CheckBox in the first DataGridView looses focus.

推荐答案

处理 DatGridView s CheckedChanged 事件,您必须先使 CellContentClick 触发(其中没有 CheckBox es当前状态!),然后调用 CommitEdit 。这将依次触发 CellValueChanged 事件,您可以使用该事件来完成工作。 这是Microsoft的疏忽。做类似以下的事情...

To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...

private void dataGridViewSites_CellContentClick(object sender, 
    DataGridViewCellEventArgs e)
{
    dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

/// <summary>
/// Works with the above.
/// </summary>
private void dataGridViewSites_CellValueChanged(object sender, 
    DataGridViewCellEventArgs e)
{
    UpdateDataGridViewSite();
}

我希望这会有所帮助。

PS检查本文 https ://msdn.microsoft.com/zh-CN/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v = vs.110).aspx

这篇关于如何检测DataGridView CheckBox事件更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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