在Winforms DataGridView中显示CellPainting事件之后的标题文本

时间:2019-02-28 12:53:47

标签: c# winforms datagridview

我有一个datagridview控件,我想在每个标题单元格的底部放置一条3px的行,看起来像

enter image description here

我已经将代码放在CellPainting中,甚至用于datagridview也是如此:

MyClassMappingAction

红线正确显示(我稍后会添加3px)。但是,标题文本现在丢失了。

我假设设置e.Handled = true;告诉不要继续绘制原始标题文本。如果将其设置为false,则红线消失。 (显然)此控件没有base.CellPainting类型的概念。

我知道我可以自己绘制文本,但随后我不得不担心对齐方式,字体...

现在是否有办法告诉系统同时执行这行操作并绘制原始标题文本?

如有必要,我愿意尝试其他方法。

1 个答案:

答案 0 :(得分:0)

没有此控件的base.CellPainting类型概念

的确; DGV除了调用基本事件外还有更多选择。

相反,您可以让它按所需顺序分别绘制零件

if (e.RowIndex < 0)   // headers
{
    e.PaintBackground(e.CellBounds, true);   // draw the default background

    Rectangle newRect = 
              new Rectangle(e.CellBounds.X, e.CellBounds.Bottom - 2, e.CellBounds.Width, 2);
    e.Graphics.FillRectangle(Brushes.Red, newRect);  // now draw the red line

    e.PaintContent(e.CellBounds);        // finally draw the text in the default way

    e.Handled = true;                   // done
}

如果禁用 dgv.EnableHeadersVisualStyles,您还可以设置在绘制列标题时要使用的许多其他属性。

对于更精细的选项,您可能需要研究MSDN

相关问题