Removing multiple items in a datagridview-related collection permanently

This is not a question, because I already answered it. But it can be useful for others.

Here's what happens:

  • Create a WinForm with Datagridview and bind a Subsonic ... Collection with over 500 objects loaded into it.
  • Add some columns to the datagrid and make at least one autosizemode = fill
  • Add logic to delete all selected columns (i.e., on the keyboard → delete)
  • Mark all entries and delete them

It takes about 30 seconds. on the high end of the PC (and scaled: 1 min per 1000 ...)

Cause:

Each time you delete a row, ListChanged events are generated that cause the datagridview to recalculate the space required for the authorized column (if someone is interested in the "internals", I linked the call schedule.

+3
source share
1 answer

Decision:

When uninstalling, disable ListChangedEvent:

mycollection.RaiseListChangedEvents = false;

// Delete multiple rows
foreach(DataGridViewRow row In dataGridView.SelectedRows) {
   dataGridView.Rows.Remove(row);
}


// After that you can re-enable the event:
mycollection.RaiseListChangedEvents = true;

// But you have to call
mycollection.ResetBindings();
//to let the datagridview perform at least one redraw.

The same quest now only takes a blink of an eye

0
source

Source: https://habr.com/ru/post/1707414/


All Articles