Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How stop CellValueChanged firing while populating?

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.

like image 818
Duane Avatar asked Aug 18 '14 08:08

Duane


1 Answers

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
}
like image 87
Eugene Podskal Avatar answered Nov 01 '22 01:11

Eugene Podskal