C# DataGridView预置RowPostPaint事件如何扩展

i逐浪 2021-06-17 12:02:17

已知DataGridView控件可以设置RowPostPaint事件,用于显示行标,脚本如下

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                                                e.RowBounds.Location.Y,
                                                dgv.RowHeadersWidth - 4,
                                                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                    dgv.RowHeadersDefaultCellStyle.Font,
                                    rectangle,
                                    dgv.RowHeadersDefaultCellStyle.ForeColor,
                                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

现在想把它添加到扩展的用户类里面,

namespace JunlinDatabaseQuery
{
    public partial class LJLdataGridView : System.Windows.Forms.DataGridView
    {
        public LJLdataGridView()
        {
        }

        public void LJLdataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                                                e.RowBounds.Location.Y,
                                                dgv.RowHeadersWidth - 4,
                                                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                    dgv.RowHeadersDefaultCellStyle.Font,
                                    rectangle,
                                    dgv.RowHeadersDefaultCellStyle.ForeColor,
                                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}

新窗体调用LJLdataGridView控件时,RowPostPaint事件没有自动生效,是哪一方面问题?

高手们能否帮忙分析指导一下?谢谢!

...全文
691 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2021-06-17
  • 打赏
  • 举报
回复 1

重载

protected override void OnRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

{....}

或显式指派

RowPostPaint+= .......

i逐浪 2021-06-17
  • 举报
回复
@xuzuning 能指导一下完整的脚本是怎么写的吗?我是新手,试了多次,没能搞定。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧