title: Draw string over cells after CellPainting event in DataGridView
body: <p>I am implementing this cell painting event for merging duplicate rows in my DataGridView. </p>
  
  <pre><code>    private void dataGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
      {
          e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
          if (e.RowIndex &lt; 1 || e.ColumnIndex &lt; 0)       return;
          if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
          {
              e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
          }
          else
          {
              e.AdvancedBorderStyle.Top = dataGrid.AdvancedCellBorderStyle.Top;
          }
      }
  
      private void dataGrid_CellFormatting(object sender, 
                                                  DataGridViewCellFormattingEventArgs e)
      {
          if (e.RowIndex == 0)
              return;
          if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
          {
              e.Value = "";
              e.FormattingApplied = true;
          }
      }
      bool IsTheSameCellValue(int column, int row)
      {
          DataGridViewCell cell1 = currentDatagrid[column, row];
          DataGridViewCell cell2 = currentDatagrid[column, row - 1];
          if (cell1.Value == null || cell2.Value == null)
          {
              return false;
          }
          return cell1.Value.ToString() == cell2.Value.ToString();
      }
  </code></pre>
  
  <p>The code above merges cells, and I have a seperate code when each cell's backcolor was being changed too. Now, I like to write text over the merged cells as a through this code:</p>
  
  <pre><code>    public void MergeCells()
      {
  
          string subjForDict = renamingSubjects(takenSubj[0]);
  
          Graphics graph = dataGrid.CreateGraphics();
  
  
          Rectangle CellRect1 = dataGrid.GetCellDisplayRectangle(dataGrid.Columns[selectedSubject0].Index, rowNum, true);
          Rectangle CellRect2 = dataGrid.GetCellDisplayRectangle(dataGrid.Columns[selectedSubject0].Index, rowNum + rowLength - 1, true);
  
          int rectHeight = 0;
          string MergedRows = String.Empty;
  
          for (int x = rowNum + 2; x &lt;= rowNum + (rowLength + 1); x++)
          {
              rectHeight += dataGrid.GetCellDisplayRectangle(dataGrid.Columns[selectedSubject0].Index, x, true).Height;
  
          }
  
          Rectangle newCell = new Rectangle(CellRect1.X, CellRect1.Y, CellRect1.Width, rectHeight);
  
          graph.DrawString("SUBJECT:", dataGrid.DefaultCellStyle.Font = new Font("Century Gothic", 8F, FontStyle.Regular, GraphicsUnit.Pixel), new SolidBrush(Color.Black), newCell.X + newCell.Width / 11, newCell.Y + newCell.Height / 10);
          graph.DrawString("ENGLISH", dataGrid.DefaultCellStyle.Font = new Font("Century Gothic", 8F, FontStyle.Regular, GraphicsUnit.Pixel), new SolidBrush(Color.Black), newCell.X + newCell.Width / 11, newCell.Y + newCell.Height / 2);
  
  
  
      }
  </code></pre>
  
  <p>The problem is that the String drawn using the MergeCells method when I call it is being overwritten by the cell painting event. Any ideas?</p>