From: Paul on
If the DataGridView datasource is a BindingList, every time that an element is added to the list, the grid repaints, which is very slow when loading a large number of element.

To fix this, create a new class which inherits from BindingList, and use an instance of that new class as the datasource.

In that new class, override the OnListChanged and OnAddingNew methods (see my example below).

Under normal operation, the override method simply calls the respective base method. During a bulk update, the override method returns without calling the base method. When the bulk update is complete, notify the grid that the underlying data has changed by calling OnListChanged(new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.Reset, 0))

In a test, it took 13.8 seconds to load 9,821 records, but by supressing the ListChanged and AddingNew events, this dropped to 0.35 seconds. That's a 40-fold improvement.

protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)
{
if (_loading)
{
// Ignore ListChanged
return;
}
// Only fire ListChanged if we aren't loading
base.OnListChanged(e);
}



hjin wrote:

How to suspend DataGridView while I am updating a lot of data?
11-Oct-07

DataGridView takes very much time if I update a lot of data

I have a DataGridView which has a DataSource (a DataTable) of 3000
rows. If I want to update one column of every rows in the DataTable,
my whole appliation freezes with any response. After a lot of time, it
comes back.

If I set the DataSource to null before changing the data and set it
back after changing. There' s no problem. But I lost the status of the
DataGridView, like current selected row, sorting, etc.

Is there any way to prevent the DataGridView to do anything while I am
updating the data in the DataTable?

EggHeadCafe - Software Developer Portal of Choice
RSVP (Rapid Sequential Visual Presentation)
http://www.eggheadcafe.com/tutorials/aspnet/5646196f-b5fa-4d95-b099-c13e46a9ad26/rsvp-rapid-sequential-vi.aspx