Вот быстрый:
![enter image description here](https://i.stack.imgur.com/z3HWz.gif)
Я использую CellPainting
для сбора текущих позиций столбцов.Этот маленький трюк позволяет мне игнорировать любую прокрутку, изменение размера столбца и т. Д.
Вот как:
List<int> colX = new List<int>();
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == 0)
{
if (e.ColumnIndex == (dataGridView1.RowHeadersVisible ? -1 : 0)) colX.Clear();
colX.Add(e.CellBounds.X);
}
}
И в событии Paint
я рисую «ячейки».
using System.Drawing.Drawing2D;
..
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
Rectangle cRect = dataGridView1.ClientRectangle;
int y0 = 0;
if (dataGridView1.ColumnHeadersVisible) y0 += dataGridView1.ColumnHeadersHeight;
int rhw = dataGridView1.RowHeadersVisible ? dataGridView1.RowHeadersWidth : 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
y0 += row.Height;
}
int y1 = cRect.Height;
using (SolidBrush brush = new SolidBrush(dataGridView1.DefaultCellStyle.BackColor))
e.Graphics.FillRectangle(brush, cRect.Left + 2, y0, cRect.Right - 4, y1 - y0 - 2);
using (Pen gridPen1 = new Pen(dataGridView1.GridColor, 1f)
{ DashStyle = DashStyle.Dot })
using (Pen gridPen2 = new Pen(dataGridView1.GridColor, 1f)
{ DashStyle = DashStyle.Solid })
{
for (int i = 0; i < colX.Count; i++)
e.Graphics.DrawLine(gridPen1, colX[i] - 1, y0, colX[i] - 1, y1);
int y = y0;
while (y < cRect.Bottom)
{
e.Graphics.DrawLine(y == y0 ? gridPen2 : gridPen1,
cRect.Left, y, cRect.Right, y);
y += dataGridView1.Rows[0].Height;
}
if (rhw > 0) e.Graphics.DrawLine(gridPen1, rhw, y0, rhw, y1);
}
}
Гораздо проще, чем код, который вы нашли, в котором есть несколько других ошибок, таких как утечка ресурсов в событии Paint
.
Примечание: я сделал псевдо-Ячейки немного выделяются, рисуя пунктирной линией.
Также обратите внимание, что код предполагает RowHeaders
.
Если у вас их нет, вы также должны Invalidate
в событии Scroll
:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.Invalidate();
}
Также сделайте DGV DoubleBuffered , чтобы избежать мерцания !!
В зависимости от ваших потребностей, вам может понадобиться Invalidate
в еще нескольких событиях, таких как:
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dataGridView1.Invalidate();
}
Другие могут включать ColumnAdded
, ColumnRemoved
..
Обновление: Я исправил незначительнуюошибка по RowHeaders
..