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

查看:45
本文介绍了如何检测 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.

我想要实现的是,当一个 DataGridView 中的 CheckBox 的选中状态发生变化时,另外两个 DataGridView 中的数据s 变化.然而,我使用的所有事件,其他网格中的数据仅在第一个 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.

推荐答案

要处理 DatGridViews CheckedChanged 事件,您必须首先获得 CellContentClick> 触发(它没有 CheckBoxes 当前状态!)然后调用 CommitEdit.这将依次触发 CellValueChanged 事件,您可以使用它来完成您的工作.这是微软的疏忽.做一些类似下面的事情...

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();
}

我希望这会有所帮助.

附言检查这篇文章 https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx

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

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