【问题标题】:Display header text after CellPainting event in winforms datagridview在 Winforms datagridview 中的 CellPainting 事件后显示标题文本
【发布时间】:2019-07-22 09:37:48
【问题描述】:

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

我已经在 CellPainting 中添加了代码,甚至是 datagridview 的代码,例如:

           if (e.RowIndex < 0)   // headers
            {

                Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y - 1 + e.CellBounds.Height, e.CellBounds.Width, 2);
                using (Brush gridBrush = new SolidBrush(Color.Red))
                {
                    e.Graphics.FillRectangle(gridBrush, newRect);
                }e.Handled = true;
            }

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

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

我知道我可以自己绘制文本,但是我不得不担心对齐、字体...

现在有没有办法告诉系统同时绘制行和绘制原始标题文本?

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

【问题讨论】:

  • e.Handled = true; 之前添加e.PaintContent(e.CellBounds); !还要在您自己的绘图前添加e.PaintBackground(e.CellBounds, true); 以获得单元格分隔符!

标签: c# winforms datagridview


【解决方案1】:

这个控件没有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

【讨论】:

  • e.PaintContent(e.CellBounds);是缺失的环节。像魅力一样工作,谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2020-12-18
  • 1970-01-01
相关资源
最近更新 更多