Clear Datagridview without firing Cellvalidating event

Asked

Viewed 165 times

0

I am trying to clean a Datagridview from C#. However when executing Rows.Clear(), before that he is running an event CellValidating which is also attached to Datagridview

You can not perform the validation before clearing the records?

  • 1

    It is probably impossible to cause the event not to be triggered. If the event has some side effect then run the Clear could be a good sign that you’re using the wrong event.

1 answer

0

You can always create a control variable, which is true before doing the Rows.Clear() and passes to false immediately afterwards:

private bool isClearing = false;

// ...

isClearing = true;
Rows.Clear();
isClearing = false;

// ...

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if(isClearing)
    {
        // return ou simplesmente e.Cancel = true;
        // dependendo das necessidades
        return;
    }

    // ...
}
  • Great, Joao. I hadn’t thought about it haha, it worked perfectly, hug

  • Glad I could help! Take advantage and mark the answer as correct ;).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.