C# DataGridView行为怪异。。。不重画留下奇怪的文物

C# DataGridView行为怪异。。。不重画留下奇怪的文物,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个使用DataGridView的WinForms应用程序。我有一个单元格的背景/文本颜色根据另一个单元格(在同一行中)的值而变化。我似乎遇到了这样的问题:当屏幕大小、移动或其他任何东西时,细胞看起来像垃圾 问题是这样的: 这是我用来更改列颜色的代码 public static void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // this is used to colo

我有一个使用DataGridView的WinForms应用程序。我有一个单元格的背景/文本颜色根据另一个单元格(在同一行中)的值而变化。我似乎遇到了这样的问题:当屏幕大小、移动或其他任何东西时,细胞看起来像垃圾

问题是这样的:

这是我用来更改列颜色的代码

public static void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // this is used to colorize the cells..
    try
    {
        DataGridView dgv = (DataGridView)sender;

        string currentColumnName = dgv[e.ColumnIndex, e.RowIndex].OwningColumn.Name;

        if (dgv.Columns.Contains(currentColumnName + "_TEXT_COLR"))
        {
            string colourString = dgv[currentColumnName + "_TEXT_COLR", e.RowIndex].Value.ToString();
            e.CellStyle.ForeColor = ColorTranslator.FromHtml(colourString);
        }
        else
            e.CellStyle.ForeColor = Color.Black;

        if (dgv.Columns.Contains(currentColumnName + "_BACK_COLR"))
        {
            string colourString = dgv[currentColumnName + "_BACK_COLR", e.RowIndex].Value.ToString();
            e.CellStyle.BackColor = ColorTranslator.FromHtml(colourString);
        }
        else
        {
            if (dgv.Columns[e.ColumnIndex].ReadOnly)
                e.CellStyle.BackColor = Color.Yellow;
            else
                e.CellStyle.BackColor = Color.White;
        }                

    }
    catch (Exception)
    {
        // ?
    }

}
我尝试刷新控件和窗体。。。不走运

有人吗?

来自:


很好

你试了什么?在网格上调用
Invalidate()
?如果必须立即刷新,甚至可能是
Refresh()
?是的,我在包含表单的ResizeEnd事件上尝试了这两种方法,但都没有做任何事情。请尝试摆脱Try…Catch,然后修复空值问题。
双缓冲区
通常是一个属性…您不能在适当的构造函数中设置它吗,甚至在designer中?@DonBoitnott仅在表单上。否则它会受到保护,因此您可以从它继承,也可以使用反射。这不是真的。该属性是从
控件继承的。例如,您可以在
面板上访问它。@DonBoitnott我第一次这样设置属性。另一个例子是:@DonBoitnott不,你不能。在控制源代码中,它是受保护的虚拟bool。
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
    Type dgvType = dgv.GetType();
    PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
    pi.SetValue(dgv, setting, null);
}