hi guys..i have a bound datagridview..and i wrote that code to trigger when the cell content is changed

private void datagridview1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
   MessageBox.Show("Content Has Changed");
}

but it is fired 7 or 8 times when the form is loaded and it is fired again whenever i choose a row..what is wrong here? and what else event i can handle to know the cell content has changed ?

Of course it will fire. The event is about to fire when ever the value in each cell changes. So that means that it will fire every time something comes into each cell.

You can avoid this by using a flag - a boolean variable, which you will set to true/false. And depending on that it will go through the code of not:

bool bChecking;

        private void YourMethod()
        {
            bChecking = true;
            //for example: here the cellValueChanged will be fired 
            //but it will not execte it becuase the boolan is set to true
            //and when it will come back, you set it back to false (let says default value):
            bChecking = false;

            //and you can do this by selecting true/false where ever you need in the code (on form load when dgv is populating, ...)
        }

        private void datagridview1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!bChecking)
            {
                MessageBox.Show("Content Has Changed");
            }
        }
Comments
helpfull
This article has been dead for over six months. Start a new discussion instead.