Reputation: 751
I'm trying to set DataGridViewRow background color depending on days left or passed from DateTime.Now value. I have written easy static class which resolves dayleft/passed to color and return this color (RowColors.GetRowColor(DataGridViewRow row). This function i use to traverse all rows in DataGridView and change background color which returns my static class.
Everything works fine, but there is one small problem - performance. I got about 1000 of Rows and coloring lasts about 4 seconds. I know, thats not so much but i want it smooth and nice.
Moreover, when i add one row i have to wait 4 seconds cause of event fires. I used Parallel loop but it seems to be drawing problem. How can i do it faster. Thanks.
private void allMatchesDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
Color rowColor=Color.LightYellow;
Parallel.For(0, allMatchesDataGridView.RowCount, i =>
{
rowColor =
RowColors.GetRowColor(
Convert.ToDateTime(allMatchesDataGridView.Rows[i].Cells[CellNameTranslator.TranslateFromDatabaseName("DateTime")].Value));
allMatchesDataGridView.Rows[i].DefaultCellStyle.BackColor = rowColor;
});
}
Upvotes: 0
Views: 795
Reputation: 54433
Since you are changing one UI element parallel will not help.
Instead you should optimize your coloring code.
First: don't update while the DGV is in live mode!
SuspendLayout();
before and ResumeLayout()
after the updates. Second:
Also, you just might consider checking out WPF, which will be a lot faster for any GUI work.
Upvotes: 1