DataGridView的复选框事件

DataGridView的复选框事件

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

问题描述

我有一个WinForms应用程序,并希望引发一些code时,嵌在 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. 触发尽快复选框被点击,但其前选中状态的变化,或

  2. 只触发一次复选框失去其焦点

  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的变化的选中状态时,在其他两个 DataGridView的变迁数据。不过我用的所有事件,在对方网格中的数据在第一的DataGridView后复选框只更改松动焦点。

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 取值的CheckedChanged 事件您必须首先获得 CellContentClick 来火(不具备复选框上课当前状态!)然后调用 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();
}

我希望这有助于。

I hope this helps.

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

P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx

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

08-13 13:55