title: Text draws out of CellBounds during CellPainting
body: <p>I'm overriding the CellPainting event for a DataGridView in an AddIn for VSTS2010. Basically I have 2 lines of data and I want the first line to be black and the second line to be grey. That all works just fine, but sometimes, and for no reason I can tell, occasionally the cell contents will be painted about 50 pixels to the left and 5 pixels up from where it should be.
  For example:
  <img src="https://i.imgur.com/AI5TX7C.jpg" alt="Example"></p>
  
  <p>The hightlighted filename is ExecutionHints.cs but all you can see is "nHints.cs"</p>
  
  <p>This happens say in just 1 VSTS session of 3, or sometimes just at some point during the afternoon for 1 session. There appears to be no pattern for when it happens.</p>
  
  <p>I've tried using e.CellBounds.X and even hardcoding "2" as the X position for the DrawString but it still happens. I've attached a debugger when it happens and the values are correct, i.e. Value == "ExecutionHints.cs", e.CellBounds.X == 1, e.ClipBounds.X == 0.</p>
  
  <p>Any ideas? Suggestions to try?</p>
  
  <p>Code:</p>
  
  <pre><code>private void grdFiles_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {
      if (grdFiles.Columns[e.ColumnIndex].Name != clmDisplay.Name || e.RowIndex &lt; 0) return;
  
      var lines = new string[] { "", "" };
      SolidBrush backColorBrush;
  
      var selected = (DataGridViewElementStates.Selected &amp; e.State) == DataGridViewElementStates.Selected;
  
      // Get back colour depending on cell contents.
      if (e.Value != null)
      {
          lines = System.Text.RegularExpressions.Regex.Split(e.Value.ToString(), Environment.NewLine);
          backColorBrush = selected ? new SolidBrush(e.CellStyle.SelectionBackColor) : GetBackBrushColor(lines[1]);
      }
      else
          backColorBrush = new SolidBrush(selected ? e.CellStyle.SelectionBackColor : e.CellStyle.BackColor);
  
      using (Brush gridBrush = new SolidBrush(this.grdFiles.GridColor))
      {
          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, ignoring alignment. 
              if (e.Value != null)
              {
                  e.Graphics.DrawString(lines[0], e.CellStyle.Font, Brushes.Black,  2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
                  e.Graphics.DrawString(lines[1], e.CellStyle.Font, selected ? Brushes.White : Brushes.Gray, 2, e.CellBounds.Y + 18, StringFormat.GenericDefault);
              }
              e.Handled = true;
          }
      }
      backColorBrush.Dispose();
  }
  </code></pre>