滚动时,DataGridView周围的绘图边框会导致问题

时间:2017-06-17 01:57:00

标签: c# vb.net datagridview border onpaint

我正在绘制一个自定义DataGridView来设置单元格边框的颜色以及主边框的颜色。

以下是我正在使用的代码:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    base.OnPaint(e);

    var g = e.Graphics;
    g.SmoothingMode = SmoothingMode.HighQuality;
    var mainRect = new Rectangle(0, 0, Width - 1, Height - 1);
    g.DrawRectangle(new Pen(Color.FromArgb(220, 220, 220)), mainRect);
}

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
    e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
    using (var p = new Pen(new SolidBrush(Color.FromArgb(220, 220, 220))))
    {
        // Bottom border
        e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom - 1), new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1));
        // Left border
        if (e.ColumnIndex > 0)
        {
            e.Graphics.DrawLine(p, new Point(e.CellBounds.Left, e.CellBounds.Top), new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1));
        }
    }
    e.Handled = true;
}

在我需要滚动DataGridView之前,一切正常。当我滚动时,控件的下边框与行一起移动,使其看起来很奇怪。

滚动前 enter image description here

滚动后 enter image description here

查看边框如何随行移动并使其在行之间有两条线?

有谁知道如何解决这个问题?我尝试在Invalidate()OnScroll(),但这会导致控件底部在滚动时闪烁,在Invalidate()重绘控件之前,您仍然可以看到额外的一行。“ p>

我还尝试使用面板技术来创建边框但是当我将其创建为自定义控件时它不能正常工作,因为面板将是父控件,因此我无法访问任何边框DataGridView方法或属性没有在我的自定义控件中明确地重新声明...

提前致谢。

0 个答案:

没有答案
相关问题