且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测DataGridView CheckBox事件更改?

更新时间:2022-06-14 03:04:42

处理 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