DataGridView cell painting issue on horizontal scroll

1 day ago 1
ARTICLE AD BOX

I have a DataGridView control on a Windows form, and I am using the CellPainting event to apply custom styling.

This here is my styling code:

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { DataGridView grid = sender as DataGridView; if (e.RowIndex == -1 && e.ColumnIndex > -1) { e.Handled = true; using (Brush b = new SolidBrush(Color.FromArgb(3, 10, 60))) { e.Graphics.FillRectangle(b, e.CellBounds); } using (Pen p = new Pen(Color.FromArgb(0, 255, 255), 2)) { e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom - 2), new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1) ); } } else if (e.ColumnIndex > -1 && e.RowIndex > -1) { using (Pen p = new Pen(Color.FromArgb(255, 255, 255), 1)) { e.Graphics.DrawLine(p, new Point(e.CellBounds.Right - 1, 0), new Point(e.CellBounds.Right - 1, grid.Height) ); } } Rectangle cr = e.CellBounds; e.PaintContent(cr); }

This works well when the app is loaded, however the vertical lines disappear when the DataGridView is scrolled horizontally by dragging the scrollbar.

If however the start of a column aligns with the start of the DataGridView, the vertical lines reappear

I feel I'm missing something small, but am at a loss....

Any help will be appreciated, thank you in advance!

Read Entire Article