I have a form, “StartForm” where the user makes a combo box choice, and then clicks a button. From there I would like to:
-
Generate an instance of another form, “MainForm”
-
Populate the DataGridView on that form with around 50 000 rows
-
Loop through each row in the datagrid view and perform some processing on it. In my case, it’s formatting the row colour based on the value of a cell, and updating some columns.
-
Display the form.
This is straight forward, however because of the large number of rows and the processing time on each one, the UI freezes when constructing the MainForm. So I need the MainForm to be constructed on a background thread, and a progress bar to fill up on the StartForm while this is going on.
I keep getting cross thread exceptions or my formatting is lost when using backgroundWorker, and control.Invoke() also seems to throw exceptions. Maybe I’m just not using these correctly…
Could someone please explain how to accomplish the above? I have tried to keep the question basic to understand so it can be helpful to others but if you would like my code then please ask.
Thanks!
I think you need to implement virtual mode for your data grid. Take a look on msdn article about implementing Virtual Mode in the DataGridView Control. Small sample:
Use
CellValueNeededevent handler to provide value for cell (i.e. select value from source). UseCellPaintingevent handler to set cell color based on cell value (usee.CellStyleproperty). If it takes long time to fill your source with data, you can do that in BackgroundWorker. But do formatting and displaying data in virtual mode – this is the best option, when you have huge amount of rows (btw consider apply some filtering – rarely users need 50000 rows of data at once).