Reputation: 41
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
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
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
Reputation: 2680
Can you remove the event handler before loading the grid, and then re-add it afterward?
Upvotes: 0