Is there a way to ‘stream’ a set of results (eg. a DataTable) from a BackgroundWorker to a DataGridView. What I want to do is to query data, and fill the results in a DataGridView as they come (like query grid results in SQL Server Management Studio). My first thought was to use a BackgroundWorker (to avoid the UI freeze effect), but there would still be a perceivable ‘lag’ as the BackgroundWorker is loading the results.
What would be the best way to go about this?
You could:
Bind the DataGridView to an initially empty DataTable.
Then, in your worker thread, use a thread-safe collection (a synchronized queue for example) and calls to Control.BeginInvoke to pass record info over to the UI thread.
In the UI thread, you’d pull items out of the queue and add corresponding rows to the DataTable. Through the magic of data binding, these would be added to the gridview.
However, by employing multithreading you immediately make your program much more likely to be broken! I haven’t tried this specific scheme, and don’t know if the GridView would be rendered effectively unusable while items are being added to it. I have populated a treeview using multithreading, and it is indeed a cool effect. However, I ended up disabling the functionality, as it introduced bugs due to not being a fully correct implementation dealing with all the possible user interactions.