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:
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?