There was a problem when scrolling or resizing a table with drawing forms, that is, everything merges, how to solve the problem.

enter image description here

Code example

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1 && e.ColumnIndex != -1) { //e.Graphics.FillRectangle(Brushes.Blue, e.CellBounds); //e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background)); //e.Handled = true; System.Drawing.Rectangle HeaderArea = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //HeaderArea = tabControl1.GetTabRect(e.RowIndex); SolidBrush fillbrush = new SolidBrush(Color.Red); System.Drawing.Rectangle buttonFilter = new System.Drawing.Rectangle(); buttonFilter.Location = new System.Drawing.Point(HeaderArea.Left, 0); buttonFilter.Size = new Size(16, 16); e.Graphics.FillRectangle(fillbrush, buttonFilter); StringFormat FormatName = new StringFormat(); FormatName.Alignment = StringAlignment.Center; FormatName.LineAlignment = StringAlignment.Center; HeaderArea.X = HeaderArea.X + 16; e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].Name, this.dataGridView1.ColumnHeadersDefaultCellStyle.Font, new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), HeaderArea, FormatName); e.Handled = true; } } private void dataGridView1_Scroll(object sender, ScrollEventArgs e) { /*System.Drawing.Rectangle rtHeader = dataGridView1.DisplayRectangle; rtHeader.Height = dataGridView1.ColumnHeadersHeight / 2; dataGridView1.Invalidate(rtHeader);*/ for (int i = 0; i < dataGridView1.Columns.Count; i++) { this.dataGridView1.InvalidateCell(i,0); } } 

    1 answer 1

    You need to add a cell background drawing:

     e.PaintBackground(e.ClipBounds, true); 

    There is no need to handle the Scroll event.


    The code can be simplified a bit by using other properties / methods.
    In addition, it is desirable to release all resources used in the code.
    I would write something like this:

     if (e.RowIndex == -1 && e.ColumnIndex != -1) { e.PaintBackground(e.ClipBounds, true); var headerArea = new Rectangle(e.CellBounds.X, e.CellBounds.Y, 16, 16); var contentArea = new Rectangle(e.CellBounds.X + 16, e.CellBounds.Y, e.CellBounds.Width - 16, e.CellBounds.Height); using (var fillbrush = new SolidBrush(Color.Red)) using (var formatName = new StringFormat()) using (var fontBrush = new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)) { e.Graphics.FillRectangle(fillbrush, headerArea); formatName.Alignment = StringAlignment.Center; formatName.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(e.Value.ToString(), dataGridView1.ColumnHeadersDefaultCellStyle.Font, fontBrush, contentArea, formatName); } e.Handled = true; } 

    Also note that starting with .NET 2.0, WinForms uses GDI by default to draw text. That is, you need to use TextRenderer.DrawText instead of the GDI + Graphics.DrawString method. Their conclusion is very small, but different.