I’ve read a number of examples on using BackgroundWorker objects to handle executing time-intensive tasks which generate results which are used to populate a DataGridView. However in my case it seems as though the act of populating the DataGridView is what’s taking the most time. I’m wondering if this is because I need to format the results (hide certain columns, check for certain flags on each row to set the color/font, etc.).
Example:
DataTable results_table;
DataGridView my_grid;
DataView my_view;
private void fillTable()
{
// Generate the results
...
// Bind the data.
my_view.Table = results_table;
my_grid.DataSource = my_view
// Format the results
my_grid.Columns[0].Visible = false;
my_grid.Columns[1].Visible = false;
my_grid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
my_grid.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
foreach (DataGridViewRow row in my_grid.Rows)
{
// Check for flags and format necessary rows.
}
}
Is this the right way to go about doing this, or is there some other way to format the results without having to iterate through each row?
You can’t actually do the formatting in another thread, as all operations that deal with UI elements must take place on the UI thread. The best you could do is do your processing and decision-making up front so that the code that actually interacts with the GUI is as simple and lean as possible.