我正在使用DataGridView,并设置了一些DataGridViewCheckBoxColumns设置,其中两个设置的ThreeState属性设置为True。
对于网格中的某些行,我只希望复选框被选中或不确定。未选中的用户不应该可用。但是,如果用户重复单击复选框,则从选中到不确定到未选中。我只想检查一下,不确定,检查,不确定等等。
是否有一种方法可以在单击(类似于标准windows窗体复选框控件上的AutoCheck属性)时阻止复选框被选中/未选中,还是有一个事件可以用于取消DataGridViewCheckBoxCell的选中更改?
我曾尝试通过编程将选中的单元格从未选中的强制到选中的或不确定的,但是UI从来没有反映这一点。
发布于 2015-09-25 19:09:32
假设您添加的任何DataGridViewCheckBoxColumn都遵循以下模式:
DataGridViewCheckBoxColumn cbc = new DataGridViewCheckBoxColumn();
cbc.ThreeState = true;
this.dataGridView1.Columns.Add(cbc);然后,只需将以下事件处理程序添加到DataGridView中,以单击和双击CheckBox:
this.dataGridView1.CellContentClick += ThreeState_CheckBoxClick;
this.dataGridView1.CellContentDoubleClick += ThreeState_CheckBoxClick;
private void ThreeState_CheckBoxClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxColumn col = this.dataGridView1.Columns[e.ColumnIndex] as DataGridViewCheckBoxColumn;
if (col != null && col.ThreeState)
{
CheckState state = (CheckState)this.dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (state == CheckState.Unchecked)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = CheckState.Checked;
this.dataGridView1.RefreshEdit();
this.dataGridView1.NotifyCurrentCellDirty(true);
}
}
}本质上,默认情况下切换顺序是:Checked => Indeterminate => Unchecked => Checked。因此,当单击事件触发Uncheck值时,将其设置为Checked,并强制网格使用新值刷新。
https://stackoverflow.com/questions/32778212
复制相似问题