I have several DataGridViews that use Graphics.DrawString() in the CellPainting() event handler to display custom values in specific columns. Normally, these values display just fine and the text looks normal (see image 1). But I also have a DataVisualization.Charting.Chart control (on a completely different WinForm) that I use to display a simple line graph. The problem: If I view the chart before viewing any of the custom-drawn DataGridView cells, the text drawn by DrawString() is noticeably fainter than it should be (see image 2).
Amazingly, if I view any of the custom-drawn grids before viewing the chart, all of the custom-drawn cells continue to look normal.
I checked the Graphics and Font objects in both test cases and they look identical. I tried creating a static Font object to use in the calls to DrawString() but that had no effect. I tried changing the FontStyle from Regular to Bold, and in the error test case the text looked like Regular rather than Bold.
It really does seem as if somehow the chart control is effecting the FontWeight used for the Font in subsequent calls to DrawString().
My application code is much too large to post but perhaps I can create a fairly simple 2 window app that demonstrates the problem if anyone thinks that would help.
I have searched in vain for anyone else reporting a problem even remotely similar to this but have found nothing. Any help/thoughts/opinions/suggestions appreciated!
EDIT: Here is the code from one of the DataGridView::CellPainting() event handlers:
private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
Rectangle cellRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1,
e.CellBounds.Width - 2,
e.CellBounds.Height - 2);
Color rowBackColor = dataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor;
Color rowForeColor = SystemColors.WindowText;
if (dataGridView.Rows[e.RowIndex].Selected)
{
rowBackColor = SystemColors.Highlight;
rowForeColor = SystemColors.HighlightText;
}
using (Brush gridBrush = new SolidBrush(dataGridView.GridColor),
foreColorBrush = new SolidBrush(rowForeColor),
backColorBrush = new SolidBrush(rowBackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top,
e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the text content of the cell
if (e.Value != null)
{
int measureId = Convert.ToInt32(e.Value.ToString());
string displayValue = ConvertToMeasure(measureId);
StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault)
{
Alignment = StringAlignment.Near
};
e.Graphics.DrawString(displayValue, e.CellStyle.Font, foreColorBrush, cellRect, stringFormat);
}
e.Handled = true;
}
}
}
}