vb.net 填充时如何停止 CellValueChanged 触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25358984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How stop CellValueChanged firing while populating?
提问by Duane
I have a form that contains several DataGridView's. They are being cleared and populated programmatically while the program is running. This is based on cells being altered in one of the DataGridViews by the user in the form. The user changing a cell triggers clearing and repopulating all the DataGridViews including the one they interact with.
我有一个包含多个 DataGridView 的表单。在程序运行时,它们会以编程方式被清除和填充。这是基于用户在表单中更改 DataGridView 之一中的单元格。用户更改单元格会触发清除并重新填充所有 DataGridView,包括与之交互的 DataGridView。
So now to my question. Can I avoid CellValueChanged being triggered everytime the DataGridViews are cleared and repopulated programmatically? Instead it should only be triggered when the user edits a cell in the form.
所以现在我的问题。每次清除 DataGridViews 并以编程方式重新填充时,我可以避免触发 CellValueChanged 吗?相反,它应该只在用户编辑表单中的单元格时触发。
I've tried searching for an answer with no success, so I hope this is not a duplicate question.
我试过寻找答案但没有成功,所以我希望这不是一个重复的问题。
回答by Eugene Podskal
I doubt that you can stop CellValueChanged event from being triggered, other than by removing your handlers(events will be still triggered, but there won't be any handlers for it):
我怀疑您是否可以阻止 CellValueChanged 事件被触发,除了删除您的处理程序(事件仍将被触发,但不会有任何处理程序):
private dgv_ThatCausesChanges_CellValueChanged(object sender, EventArgs e)
{
this.otherDGV.CellValueChanged -= this.dgv_OtherDGVCellValueChanged;
try // To make sure that handlers are reinstatiated even on exception thanks @Steve
{
// Change other DGVs
}
finally
{
this.otherDGV.CellValueChanged += this.dgv_OtherDGVCellValueChanged;
}
}
Or as alternative solution just add some flag, that will be checked in every handler:
或者作为替代解决方案,只需添加一些标志,这将在每个处理程序中进行检查:
private bool IsChanging;
private dgv_ThatCausesChanges_CellValueChanged(object sender, EventArgs e)
{
this.IsChanging = true;
try // To make sure that handlers are reinstatiated even on exception thanks @Steve
{
// Change other DGVs
}
finally
{
this.IsCHanging = false;
}
}
private dgv_OtherDGVCellValueChanged(object sender, EventArgs e)
{
if (this.IsChanging)
return;
// Handle changes
}
回答by Duane
With thanks to @Eugene and @Steve I have come to a solution. I have model classes that call update methods in my GUI class. When these update methods are called, I remove the event handler so that clearing and refreshing the DataGridView doesn't result in several calls to CellValueChangedas seen below:
感谢@Eugene 和@Steve,我找到了一个解决方案。我的 GUI 类中有调用更新方法的模型类。调用这些更新方法时,我会删除事件处理程序,以便清除和刷新 DataGridView 不会导致多次调用,CellValueChanged如下所示:
public void updateSomeDataGridView(String[][] someData)
{
this.dataGridView1.CellValueChanged -= this.DataGridView1_CellValueChanged;
dataGridView1.Rows.Clear();
//Repopulate DataGridView with new data.
dataGridView1.Refresh();
this.dataGridView1.CellValueChanged += this.DataGridView1_CellValueChanged;
}

