Ive written a customised version of the Microsoft .net 2 DataGridView. I did this by inheriting the MS one and setting some features in the inherited one to make the style consistant throughtout my app (alternating line colors, single row selection by default, yada yada).

I was asked to add icons to the row header (on the left of the grid) to symbolise the state of some of the data in the grid. I did this by overriding the OnCellPainting method. heres roughly how it looks (ive removed some of the different STATUSes as they all do effectively the same)

override void OnCellPainting(DataGridViewCellPaintingEventArgs e)


e.Handled = false;

if ((e.ColumnIndex == -1) && (e.RowIndex > -1))


e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), e.CellBounds);

e.Graphics.DrawRectangle(new Pen(new SolidBrush(GridColor)), e.CellBounds);

System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes();

imageAttribute.SetColorKey(Color.Magenta, Color.Magenta, System.Drawing.Imaging.ColorAdjustType.Bitmap);

Bitmap b = null;

if (this["STATUS", e.RowIndex].Value.ToString() == "WIP")

b = Properties.Resource.WIP.ToBitmap();

if (this["STATUS", e.RowIndex].Value.ToString() == "QA REQUIRED")

b = Properties.Resource.QAREQUIRED.ToBitmap();

if (this["STATUS", e.RowIndex].Value.ToString() == "PENDING")

b = Properties.Resource.PENDING.ToBitmap();

if (b != null)


e.Graphics.DrawImage(b, new System.Drawing.Rectangle(e.CellBounds.Left + 3, e.CellBounds.Top + 3, b.Size.Width, b.Size.Height), 0, 0, b.Width,

b.Height, GraphicsUnit.Pixel, imageAttribute);

e.Handled = true;





Now this works absolultely fine, most of the time. However there is a single case where it doesnt work, and unfortunately it is on a remote PC and I cannot replicate the problem. The grid is embedded in 2 forms in my DLL. 1 form is called in 1 application, the other in another application. 2 forms are almost identical, and the grid was created by simply dropping it onto the form in both.

In PCs with Intel 915GM graphics processors, it works fine in 1 app, but not in the other. On all other PCs Ive tested on (all of which have different graphics chips) the code runs fine in both apps. I dont think I'm doing anything too weird that should make it fail, but with the 1 graphics chipset the entire grid simply renders solid black, except for the 1st top left cell (Well, the 1st top left cell under the column headers). If Ive not been clear I can probably post some pictures somewhere if needbe. The end user has tried updating the system to use the latest WHQL drivers to no avail. As I say, it renders fine in 1 app, but not in another, despite being created the exact same way and using the exact same control.

Is there any other way of accomplishing what I need to do in another way All i need to do is render a small 16x16 icon to the leftmost cell for a given row. Can anyone see anything obviously wrong with the above code that might make it fail and therefore fail to render every cell after the 1st one is there any gotchas I should be aware of with using the OnCellPainting

Any thougths on this strange situation would be extremely good.



Re: Windows Forms General OnCellPainting problem in DataGridView

Zhi-Xin Ye - MSFT

Try handle the RowPostPaint event instead,

Re: Windows Forms General OnCellPainting problem in DataGridView


Hi, thanks for the response. I'll not be able to get someone to test this for a while, as we simply removed the icons for this release of our software. Its currently going through QA now and I dont want to confuse testers with a different version. I'll post back as soon as I can confirm it works/fails.