DataGridView横向滚动时的单元格绘制问题

编程语言 2026-07-11

我在一个Windows窗体上有一个 DataGridView 控件,并且我正在使用 CellPainting 事件来应用自定义样式。

下面是我的样式代码:

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    DataGridView grid = sender as DataGridView;

    if (e.RowIndex == -1 && e.ColumnIndex > -1)
    {
        e.Handled = true;

        using (Brush b = new SolidBrush(Color.FromArgb(3, 10, 60)))
        {
            e.Graphics.FillRectangle(b, e.CellBounds);
        }

        using (Pen p = new Pen(Color.FromArgb(0, 255, 255), 2))
        {
            e.Graphics.DrawLine(p, 
                new Point(0, e.CellBounds.Bottom - 2), 
                new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1)
                );
        }
    }
    else if (e.ColumnIndex > -1 && e.RowIndex > -1)
    {
        using (Pen p = new Pen(Color.FromArgb(255, 255, 255), 1))
        {
            e.Graphics.DrawLine(p,
                new Point(e.CellBounds.Right - 1, 0),
                new Point(e.CellBounds.Right - 1, grid.Height)
                );
        }
    }

    Rectangle cr = e.CellBounds;
    e.PaintContent(cr);
}

这段样式在应用程序加载时工作得很好,但通过拖动滚动条水平滚动 DataGridView 时,垂直线会消失。

然而,如果某一列的起点与 DataGridView 的起点对齐,垂直线就会重新出现。

我觉得自己漏掉了一个小细节,但就是想不出来……

如果能得到帮助,我将不胜感激,先行致谢!

解决方案

要绘制垂直线(在我的示例中是红色),你可以使用 DataGridViewPaint 事件。它在每次刷新时只触发一次,而不像 CellPainting 那样,对每个单元格的渲染都会触发。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

    DataGridView grid = sender as DataGridView;

    if (e.RowIndex == -1 && e.ColumnIndex > -1)
    {
        e.Handled = true;

        using (Brush b = new SolidBrush(Color.FromArgb(3, 10, 60)))
        {
            e.Graphics.FillRectangle(b, e.CellBounds);
        }

        using (Pen p = new Pen(Color.FromArgb(0, 255, 255), 2))
        {
            e.Graphics.DrawLine(p,
                new Point(0, e.CellBounds.Bottom - 2),
                new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1)
                );
        }
    }
    //else if (e.ColumnIndex > -1 && e.RowIndex > -1)
    //{
    //    using (Pen p = new Pen(Color.Red, 1))
    //    {
    //        e.Graphics.DrawLine(p,
    //            new Point(e.CellBounds.Right - 1, 0),
    //            new Point(e.CellBounds.Right - 1, grid.Height)
    //            );
    //    }
    //}

    Rectangle cr = e.CellBounds;
    e.PaintContent(cr);

}

private void dataGridView1_Paint(object sender, PaintEventArgs e)
{

    DataGridView grid = sender as DataGridView;

    using (Pen redPen = new Pen(Color.Red, 1))
    {
        foreach (DataGridViewColumn col in grid.Columns)
        {
            if (col.Visible)
            {
                Rectangle rect = grid.GetColumnDisplayRectangle(col.Index, false);
                if (rect.Width > 0) e.Graphics.DrawLine(redPen, rect.Right - 1, 0, rect.Right - 1, grid.ClientSize.Height);
            }
        }
    }

}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章