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.
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.
I've tried searching for an answer with no success, so I hope this is not a duplicate question.
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):
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With