Winform datagridview画斜线表头

这篇博客介绍了如何在Winform的datagridview中绘制斜线表头。通过自定义绘制事件`PaintLine`,根据给定的参数`type`、`name1`和`name2`,在特定单元格上绘制斜线并显示两个名称。内容包括使用`Graphics`对象进行绘图,设置画笔和刷子颜色,以及定位和绘制文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      

  /// <summary>
        /// 画斜线表头
        /// </summary>
        /// <param name="e"></param>
        /// <param name="type"></param>
        /// <param name="name1"></param>
        /// <param name="name2"></param>
        void PaintLine(DataGridViewCellPaintingEventArgs e, string type, string name1, string name2)
        {          
            if ((type == "1" && e.ColumnIndex == 1 && e.RowIndex == -1) || (e.ColumnIndex == 0 && e.RowIndex == -1 && type == "2"))
            {
                Graphics gp = e.Graphics;
                Rectangle rec = e.CellBounds;
                Brush gridBrush = new SolidBrush(Color.FromArgb(208, 215, 229));
                Pen gridLinePen = new Pen(gridBrush);
                gp.DrawLine(gridLinePen, new Point(rec.X, rec.Y), new Point(rec.X + rec.Width, rec.Y + rec.Height));
              
                float x = 55;
                if (type == "1")
                    x = x + 30;
                gp.DrawString(name1, e.CellStyle.Font, Brushes.Black, new PointF(rec.X + 12, rec.Y + 22));
                gp.DrawString(name2, e.CellStyle.Font, Brushes.Black, new PointF(rec.X + x, rec.Y + 5));
            }

        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值