I have found that when using a BindingList as the data source for a DataGridView, the cause of the slowdown is that the BindingList fires a
ListChanged event every time the list is changed. The DGV responds by updating/repainting itself every time this event is fired.
A solution that works for me is to create a class that inherits from
BindingList , and then override the
OnListChanged and
OnAddingNew methods. During normal operation, these overrides simply call their respective base methods. However, when I want to perform a bulk load operation, I set my _
loading variable to true so that the override methods do not call their base methods.
Here is my
OnListChanged override method. My
OnAddingNew is the same.
protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)<br />
{<br />
if (_loading)<br />
{<br />
// Suppress ListChanged events during bulk load<br />
return;<br />
}<br />
// Only fire ListChanged if we aren't loading<br />
base.OnListChanged(e);<br />
}<br />
When the bulk load is complete, I set
_loading to false, and call the
OnListChanged method with the System.ComponentModel.ListChangedType.
Reset argument so that the DGV will reload with the changed data
OnListChanged(new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.<strong>Reset</strong>
, 0));
Using this method has made a huge difference. Without
_loading = false , it takes 13.8 seconds to load 9,821 records. With
_loading = true , it takes 0.35 seconds. That is about
40 times faster !