Question

I have a C# WinForms app that contains a BindingList which is populated with widget objects like this:

BindingList<Widget> widgetsList = new BindingList<Widget>();

A widget has three properties (one int, one string and one bool) and implements INotifyPropertyChanged. The widgets get added to the BindingList like this:

for (int i = 0; i < 100000; i++) // takes < 1/10 of a second to execute
{
   widgetsList.Add(new Widget(intValue, stringValue, boolValue));
}

And finally, I set the BindingList as the data source for a DataGridView (actually three DataGridView controls):

dataGridView1.DataSource = widgetsList;

This code is working well. However, if my user makes changes in the DataGridView and then wants to discard those changes and re-load the original data, I'm having issues. Right now I have a button to do this and the code in the button's Click event handler looks like this:

dataGridView1.Rows.Clear() // very fast

widgetsList.Clear() // also very fast

    for (int i = 0; i < 100000; i++) // takes 18.6 seconds to execute
    {
       widgetsList.Add(new Widget(intValue, stringValue, boolValue));
    }

As you can see, this loop executes substantially slower (18.6s vs < .1s) than the same loop above. It does appear to work but I'm trying to figure where the bottleneck is and how to remove it. Am I missing something?

Was it helpful?

Solution

Posting answer from comment above: You need to add a BindingSource between the grid and the binding list. This will allow you to suspendbinding while reloading the source list and allow the grid to hold off rendering until the datasource is fully updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top