I understand how to use delegates to update controls on the main control thread, works like a charm. My problem here is if I'm adding a large dataset (say 2000 items) to a bound datagridview it takes 5-8 seconds for the grid to populate and during that 5-8 seconds the whole gui is locked. How can I update the datagridview such that it doesn't lock the user interface?
To be clear, the problem isn't that I'm doing a slow query to a database and the UI is blocking on that, I already have the dataset object[] and adding the array of objects to a BindingList which the datagrid is bound to so:
BindingList<object> dataProvider = new BindingList<object>();
DataGridView gridView = new DataGridView();
gridView.DataSource = dataProvider;
...stuff happens...
object[] source = dataSet; //of 2000 items
foreach (object item in source) { //this foreach blocks
dataProvider.Add(item);
}
I tried various things (that I knew wouldn't work but figured I'd see) like creating a delegate that did the dataProvider.Add(), but that didn't matter since it still had to happen on the control thread.
Any help would be much appreciated, thanks! Sam
EDIT: A couple good suggestions revolved around building the BindingList first and then setting the gridView.DataSource. While this works (it updates the grid instantly) the only way I see to add more data is to create another new BindingList, do a gridView.DataSource.copyTo() (to get the existing data) and add the new data on top of that then set the gridView.DataSource to the new BindingList. This won't work for me since the objects in my list are not static, they are each uploading data to a server async. and copying them to a new bindinglist would cause problems.