[英]How do I trigger the cell value change event in a DataGridView only when done manually?
所以我知道这个问题已经解决了( 当以编程方式更改值时,不要在DataGridView中触发单元格值更改事件 ),但是提供的答案没有足够的文档记录,并且无法正常工作。
本质上,我通过验证引入的数据来处理datagridview的单元值更改事件,如果数据超出指定范围,则将其更改为适合的值。 问题就出在这里。 当我以编程方式执行此操作时,它将触发该事件两次; 我不希望它这样做。
有任何想法吗?
提前致谢!
当您需要以编程方式更改值时,可以禁用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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.