以编程方式检查刚刚未选中的DataGridView CheckBox [英] Programmatically check a DataGridView CheckBox that was just unchecked
问题描述
我知道之前也曾问过类似的问题,但是没有一种解决方案对我有帮助。
I am aware that similar questions have been asked before, but none of the solutions are helping me.
我在未绑定的DataGridView中有一个DataGridViewCheckBoxColumn。
在 CellContentClick
事件中,当如果未选中CheckBox,则根据用户在DataGridView后面的业务规则提示用户是否要继续执行此操作,如果他们选择不继续,则要重新检查CheckBox。
I have a DataGridViewCheckBoxColumn in an unbound DataGridView.
In the CellContentClick
event, when a CheckBox is unchecked, I am prompting the user whether they want to continue with this operation according to the business rules behind the DataGridView and, if they choose not to continue, I want to re-check the CheckBox.
是对CheckBox的重新检查不起作用。
It is this re-checking of the CheckBox that is not working.
这是我的代码:
private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dgvPeriods.Columns["colSelected"].Index)
{
dgvPeriods.CommitEdit(DataGridViewDataErrorContexts.Commit);
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvPeriods[e.ColumnIndex, e.RowIndex];
if (chk.Value = chk.FalseValue)
{
If (MessageBox.Show("Continue with this Operation?", "Continue", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
chk.Value = chk.TrueValue;
return;
}
}
}
}
设置了单元格的值,但在视觉上未选中CheckBox。
The value of the cell is being set, but visually the CheckBox is not checked.
如果为 TrueValue
和 FalseValue
(布尔值与字符串),我尝试调用 Refresh()
,我尝试调用 CommitEdit()
,尝试使用 CheckState.Checked
。
If have tried different types for the TrueValue
and FalseValue
(booleans vs strings), I have tried calling Refresh()
, I have tried calling CommitEdit()
, I have tried using CheckState.Checked
.
如何可视地重新检查CheckBox?
What can I do to visually re-check the CheckBox ?
推荐答案
您可以在之后立即提交修改使用(正确的) CellContentClick 事件system.windows.forms.datagridview.endedit rel = nofollow noreferrer> EndEdit()方法,因此 CellValueChanged
1 事件也会立即引发当前单元格失去焦点后的。
You can commit the edit immediately after a CellContentClick
event is raised, using the (proper) EndEdit() method, so the CellValueChanged
1 event is also raised immediately instead of after the current Cell loses focus.
在此处评估新值:由于该值已更改,因此当前值与上一个值相反,这是一个 bool
列。
Evaluate here the new Value: since the Value has changed, it's intended that the current value is the opposite of the previous, give that this is a bool
Column.
此时,如果用户确认所做的选择,则重置该值并调用 RefreshEdit()以便以当前状态重新绘制CheckBox。
At this point, if the User confirms the choice made, you reset the value and call RefreshEdit() to redraw the CheckBox in its current state.
注意:您的DataGridView的行为可能取决于操作的上下文。
Note: the behavior of your DataGridView may depend on the context of your operations.
private void dgvPeriods_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;
bool newValue = (bool)dgvPeriods[e.ColumnIndex, e.RowIndex].Value;
if (!newValue) {
if (MessageBox.Show("Continue with this Operation?", "Continue",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
dgvPeriods[e.ColumnIndex, e.RowIndex].Value = true;
dgvPeriods.RefreshEdit();
}
}
}
private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// You need to evaluate whether EndEdit() applies to just this Column
if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;
dgvPeriods.EndEdit();
}
1-请注意,此事件是 really ,在上一个事件处理程序中的代码完成之前立即
1 - Note that this event is raised really immediately, before the code in the previous event handler completes
这篇关于以编程方式检查刚刚未选中的DataGridView CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!