title: Drawing border around DataGridView causes issues when scrolling
body: <p>I have a custom DataGridView that I am drawing to set the colour of the cell border and also the colour of the main border.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  {
      base.OnPaint(e);
  
      var g = e.Graphics;
      g.SmoothingMode = SmoothingMode.HighQuality;
      var mainRect = new Rectangle(0, 0, Width - 1, Height - 1);
      g.DrawRectangle(new Pen(Color.FromArgb(220, 220, 220)), mainRect);
  }
  
  protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
  {
      e.Paint(e.CellBounds, DataGridViewPaintParts.All &amp; ~DataGridViewPaintParts.Border);
      using (var p = new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))))
      {
          // Bottom border
          e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom - 1), new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1));
          // Left border
          if (e.ColumnIndex &gt; 0)
          {
              e.Graphics.DrawLine(p, new Point(e.CellBounds.Left, e.CellBounds.Top), new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1));
          }
      }
      e.Handled = true;
  }
  </code></pre>
  
  <p>It was all working fine until I needed to scroll through the DataGridView. When I scroll, the bottom border of the control moves along with the row, making it look weird.</p>
  
  <p><strong>Before Scroll</strong>
  <a href="https://i.stack.imgur.com/9YSBC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9YSBC.png" alt="enter image description here"></a></p>
  
  <p><strong>After Scroll</strong>
  <a href="https://i.stack.imgur.com/hUABc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hUABc.png" alt="enter image description here"></a></p>
  
  <p>See how the border gets moved with the row and causes it to have two lines between the rows?</p>
  
  <p>Does anyone know how I can fix this? I tried to <code>Invalidate()</code> on <code>OnScroll()</code> but that causes the bottom of the control to flicker when scrolling and you can still see the extra lines for a brief second before <code>Invalidate()</code> redraws the control.</p>
  
  <p>I also tried to use the panel technique to create the border but that doesn't work properly when I am creating it as a custom control as the panel will be the parent control so I can't access any of the DataGridView methods or properties without explicitly re-declaring them in my custom control...</p>
  
  <p>Thanks in advance.</p>