Removing items is slow

Removing items is slow

Old forum URL: forums.lhotka.net/forums/t/612.aspx


ajj3085 posted on Thursday, July 13, 2006

I have a DataGridView, and am using the following code to remove selected rows:

private void RemoveNumbersBtn_Click( object sender, EventArgs e ) {

       List<PhoneNumber> selectedRows;

       PhoneNumber num;

       selectedRows = new List<PhoneNumber>();

       foreach ( DataGridViewCell cell in

           MergeNumbersDataGridView.SelectedCells ) {

           num = (PhoneNumber)cell.OwningRow.DataBoundItem;

           if ( !selectedRows.Contains( num ) ) {

                selectedRows.Add( num );

           }

       }

       MergeNumbersDataGridView.SuspendLayout();

       foreach( PhoneNumber number in selectedRows ) {

           MergeNumbersBindingSource.Remove( number );

       }

       MergeNumbersDataGridView.ResumeLayout();

  }

It works, but seems like it could be quicker; it takes about half a second for the grid to update.  Any ideas?

Thanks
Andy

RockfordLhotka replied on Thursday, July 13, 2006

Wouldn't you want to tell the bindingsource to not raise events during this process? Then tell it to resume event processing at the end and also tell it to reset the bindings.

Otherwise what I think happens is that the collection raises lots of listchanged events, and data binding refreshes much of the form (even though you've stopped display updates for the grid). If you cut off the events at the bindingsource perhaps it would stop a lot of extra data binding work.

ajj3085 replied on Friday, July 14, 2006

I actually ended up turning off list changed events at the collection level, so that the events are raised at all using the collections RaiseListChangedEvents flag.  Unfortunatly this is a dupe of another post, I'm not sure how two got created.

Copyright (c) Marimer LLC