有没有办法停止 DataGridViewCheckBoxColumn 自动检查点击?
Is there a way to stop DataGridViewCheckBoxColumn auto checking on click?
我正在使用 DataGridView 并设置了一些 DataGridViewCheckBoxColumns,其中两个将 ThreeState 属性 设置为 True。
对于网格中的某些行,我只希望复选框处于选中状态或不确定状态。 Unchecked 不应该对用户可用。但是,如果用户重复单击该复选框,它就会从选中变为不确定再变为未选中。我只是想让它检查、不确定、检查、不确定等。
有没有办法在单击时阻止复选框 checked/unchecked(类似于标准 windows 表单复选框控件上的 AutoCheck 属性),或者是否有事件我可以用来取消选中的 DataGridViewCheckBoxCell 更改吗?
我试图以编程方式强制选中的单元格从未选中变为选中或不确定,但 UI 从未反映过这一点。
假设您添加的任何 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
并强制使用新值刷新网格。
我正在使用 DataGridView 并设置了一些 DataGridViewCheckBoxColumns,其中两个将 ThreeState 属性 设置为 True。
对于网格中的某些行,我只希望复选框处于选中状态或不确定状态。 Unchecked 不应该对用户可用。但是,如果用户重复单击该复选框,它就会从选中变为不确定再变为未选中。我只是想让它检查、不确定、检查、不确定等。
有没有办法在单击时阻止复选框 checked/unchecked(类似于标准 windows 表单复选框控件上的 AutoCheck 属性),或者是否有事件我可以用来取消选中的 DataGridViewCheckBoxCell 更改吗?
我试图以编程方式强制选中的单元格从未选中变为选中或不确定,但 UI 从未反映过这一点。
假设您添加的任何 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
并强制使用新值刷新网格。