views:

35

answers:

2

The datagridview rowsremoved event gets called every time the data gets loaded. It also makes sense to a certain extent that every time the data loads, the existing rows are removed. so technically the event should get called.

But how do i differenciate that from the actual delete button getting pressed. I don't think the key events should be used, that wouldn't be a clean approach.

Any help will be most appreciated.

+1  A: 

Can you remove the event handler before loading the grid, and then re-add it afterward?

joelt
I have been trying to do that but which event do I re-add it afterward?
Nilotpal Das
You re-add the RowsRemoved event, in whatever method you load the grid. Like RemoveHandler grid.RowsRemoved, AddressOf someFunction;LoadData(); AddHandler grid.RowsRemoved, AddressOf someFunction;(sorry, I don't know how to code format in a comment box)
joelt
+1  A: 

You could have a private boolean variable to know when you are loading and when you are not.

private bool IsLoading { get; set; }

In the form Load event, you set the variable

private void MyForm_Load(object sender, EventArgs e)
{
    this.IsLoading = true;
// do stuff
    this.IsLoading = false;
}

private void DataGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
            if (this.IsLoading)
            {
                return;
            }

   //do stuff
}
sheir