DataGridView复选框事件 [英] DataGridView checkbox event

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

问题描述

我有一个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 事件。 这是微软的一个监督。做一些像下面这样的事情...

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/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v = vs.110).aspx

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

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