プログラムで値を変更する必要がある場合は、CellValueChangedEventを無効にできます。値を変更した後は、CellValueChangedEventなどを再度有効にしてください。
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//check whether the value is valid
var specifiedMax = 100;
var compareValue = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
if (compareValue > specifiedMax)
{
//disable the cellvaluechanged event before changing the value
this.dataGridView1.CellValueChanged -= this.dataGridView1_CellValueChanged;
try
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 100;
}
finally
{
//enable the cellvaluechanged event again
this.dataGridView1.CellValueChanged += this.dataGridView1_CellValueChanged;
}
}
}
出典
2017-09-19 14:45:58
Riv