0

I have WinForms application that uses a DataGridView. I have one of the cells background/text colour changing depending on another cells (in the same row) values. I seem to be running into problems where the cells look like garbage when the screen sizes, moves or whatever.

This is what the problem looks like:

enter image description here

This is the code I use to change the column colours.

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)
    {
        // ?
    }

}

I've tried refreshing the control and the form... no luck.

Anyone?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
carny666
  • 2,325
  • 2
  • 18
  • 27

1 Answers1

0

From here:

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);
}

Works great!!

Community
  • 1
  • 1
carny666
  • 2,325
  • 2
  • 18
  • 27
  • `DoubleBuffered` is typically a property...can't you just set it in the appropriate constructor, or even in designer? – DonBoitnott Jan 30 '14 at 17:11
  • @DonBoitnott Only on forms. It's protected otherwise, so you either inherit from it, or use reflection. – LarsTech Jan 30 '14 at 17:12
  • @LarsTech Not true. That property is handed down from `Control`. You can access it on a `Panel` for instance. – DonBoitnott Jan 30 '14 at 17:14
  • @DonBoitnott First time I ever set a property this way. Another example was: http://stackoverflow.com/questions/118528/horrible-redraw-performance-of-the-datagridview-on-one-of-my-two-screens – carny666 Jan 30 '14 at 17:14
  • @DonBoitnott No, you can't. It's `protected virtual bool` in the Control source code. – LarsTech Jan 30 '14 at 17:16
  • @carny666 That last example is making your own class called `CustomDataGridView` which inherits from the stock `DataGrid View`. And then _setting the property_, as I first suggested. Probably the cleanest alternative, IMO. – DonBoitnott Jan 30 '14 at 17:16
  • @DonBoitnott Try what? Putting a panel control on a form? No DoubleBuffered property: `Cannot access protected member 'System.Windows.Forms.Control.DoubleBuffered'` – LarsTech Jan 30 '14 at 17:18
  • @LarsTech I see it now. I rarely deal in stock controls, so all the code I have in front of me are _inherited_, so access to `DoubleBuffered` is available. My mistake. – DonBoitnott Jan 30 '14 at 17:19