3

我在 C# winforms 4.0 中有一个 datagridview。我正在对背景颜色和边框进行一些自定义单元格绘画。这是我在 CellPainting 事件中的代码:

 //Background color
 if (e.RowIndex / 3 % 2 == 0 && e.RowIndex > -1)
     e.CellStyle.BackColor = Color.LightGray;

 //Bottom border
 if (e.RowIndex % 3 == 2)
 {
    using (Pen p = new Pen(Brushes.Black))
    {
       p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
       e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom - 1), 
                              new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1) );
    }
    e.PaintContent(e.CellBounds);
 }

这就是我的datagridview的样子(我不能发布图片,所以这里有一个链接) http://i.imgur.com/hLR3JjV.png

如您所见,背景颜色在我的所有单元格中都有效,但是对于仅部分显示在 datagridview 中的单元格,边框没有绘制。例如,我的图像是 Column4 中每一行的单元格

有人可以帮我弄清楚我能做些什么来让部分显示的单元格绘制底部边框吗?

4

1 回答 1

2

它被正常的单元格绘制代码再次透支。您必须使用它e.Handled = true;来防止这种情况发生。这需要你做更多的工作,你还需要绘制背景。顺便说一句,永远不要更改绘制事件处理程序中的属性。并且从左到右绘制,而不是0。(e.PaintBackground(e.CellBounds, true);在the之前usinge.Handled = true;之后drawContent)很值得以后发现。

我希望这能解决你的问题!祝你今天过得愉快!

于 2014-12-10T09:51:03.943 回答