English 中文(简体)
与DataGridView绑定的集合中删除多个项需要很久的时间。
原标题:
  • 时间:2009-04-30 12:13:09
  •  标签:

This is not a question, cause I already answerd it. But it may be helpful to others, too.

这是发生的情况:

  1. Create a WinForm with a Datagridview and bind a Subsonic ...Collection with more then 500 objects loaded to it
  2. Add some columns to the datagrid and make at least one autosizemode = fill
  3. Add logic to delete all selected columns (i.e. on keypress -> delete)
  4. Mark all records and delete them

This should take about 30 sec. on a high end pc (and scales up: 1 min for 1000 ...)

Cause:

Everytime you delete a row the collections ListChanged event is fired which causes the datagridview to recalculate the space needed for the autosized column (if someone is interested in the "internals" I attached a call graph.

最佳回答

解决方案:

删除时,禁用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.

同样的任务现在只需眨眼间就可以完成。

问题回答

暂无回答




相关问题
热门标签