datagridviewcell-formattingdatagridviewcellstyle

Datagridview Cellformating slows form and causes flicker


I have looked everywhere to try and find a fix for this. I have a datagridview for showing estimates, my organization wanted some formatting based on the estimator and the estimate progress. Below is my code the issue I am having is that the other controls on my form flicker when this grid is visible and double buffering those controls or the form hasn't made a difference. I was hoping by limiting the applying of the formatting to once per row would help this but it is still flickering.

if (DGVR.Cells["Estimate_Progress"].ColumnIndex == colIndex)
{
if (DGVR.Cells["Color_Value"].Value != null)
{
    if (DGVR.Cells["Color_Value"].Value.ToString() != "")
    {
        string colorVal = DGVR.Cells["Color_Value"].Value.ToString();
        DGVR.DefaultCellStyle.BackColor = Color.FromArgb(Convert.ToInt32(colorVal));
    }
    else
    {
        DGVR.DefaultCellStyle = DGV.RowsDefaultCellStyle;
    }
}
else
{
    DGVR.DefaultCellStyle = DGV.RowsDefaultCellStyle;
}
if (DGVR.Cells["Estimate_Progress"].Value != null)
{
    string progressVal = DGVR.Cells["Estimate_Progress"].Value.ToString();
    if (progressVal == "Not Bidding")
    {
        Font font = DGVR.DefaultCellStyle.Font;
        DGVR.DefaultCellStyle.Font = new Font(font, FontStyle.Strikeout);
    }
    else
    {
        Font font = DGV.DefaultCellStyle.Font;
        DGVR.DefaultCellStyle.Font = new Font(font, FontStyle.Regular);
    }
}
else
{
    Font font = DGV.DefaultCellStyle.Font;
    DGVR.DefaultCellStyle.Font = new Font(font, FontStyle.Regular);
}
}


Solution

  • the problem was I was modifying the row DefaultCellStyle instead of e.CellStyle

    Font font = DGV.DefaultCellStyle.Font;
    if (DGVR.Cells["Estimate_Progress"].Value != null)
    {
        string progressVal = DGVR.Cells["Estimate_Progress"].Value.ToString();
        if (progressVal == "Not Bidding")
        {
            e.CellStyle.Font = new Font(font, FontStyle.Strikeout);
        }
    }
    if (DGVR.Cells["Color_Value"].Value != null)
    {
        if (DGVR.Cells["Color_Value"].Value.ToString() != "")
        {
            string colorVal = DGVR.Cells["Color_Value"].Value.ToString();
            e.CellStyle.BackColor = Color.FromArgb(Convert.ToInt32(colorVal));
        }
    }