Nilotpal Das
Nilotpal Das

Reputation: 41

datagridview rowsremoved event gets called every time data loads

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.

Upvotes: 4

Views: 3522

Answers (4)

Jeff
Jeff

Reputation: 165

This is how we check to make sure the data has rows first:

If dataGridView.SelectedRows.Count > 0 Then
End If

See also: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.userdeletingrow.aspx

Upvotes: 2

Dunc
Dunc

Reputation: 18922

You could use the UserDeletedRow event instead.

Upvotes: 5

sheir
sheir

Reputation: 393

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
}

Upvotes: 1

joelt
joelt

Reputation: 2680

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

Upvotes: 0

Related Questions