追加したものが次のパターンに従っていると仮定し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
、グリッドを強制的に新しい値で更新します。