【发布时间】:2014-12-07 16:56:41
【问题描述】:
我在 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 中每一行的单元格
谁能帮我弄清楚我可以做些什么来让部分显示的单元格绘制底部边框?
【问题讨论】:
-
它被正常的单元格绘制代码再次透支。您必须使用
e.Handled = true;来防止这种情况发生。这需要你做更多的工作,你还需要绘制背景。顺便说一句,永远不要更改绘制事件处理程序中的属性。并且从左到右绘制,而不是 0。 -
我没有看到这个 - 这应该放在一个答案中,imo。虽然它似乎只需要 2 行(
using之前的e.PaintBackground(e.CellBounds, true);和drawContent之后的e.Handled = true;),但它非常值得在未来找到.. -
非常感谢 TaW!那成功了。还要感谢汉斯!有没有办法可以将此标记为已回答?
标签: c# datagridview custom-painting