重绘DataGridView标头

最近突然想在DataGridView标头放置一个CheckBox,我就想着重写下DataGridViewColumnHeaderCell
抱着试试的心态结果真的是可以的
下面是源码:(如果有看不懂的可以加 源码分享群 81582487 来问我)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;

namespace WindowsDemo
{

      public  class MyDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell
    {

        public delegate void CheckBoxClick(DataGridViewCellMouseEventArgs e);
        public delegate void ButtonClick(DataGridViewCellMouseEventArgs e);
        public event CheckBoxClick checkboxClick = null;
        public event ButtonClick buttonClick = null;

        public MyDataGridViewColumnHeaderCell()
            : base()
        {
           
        }
        private Rectangle ChecboxLocation
        {
            get;
            set;
        }

       private Rectangle ButtonLocation { get; set; }

       public CheckBoxState CheckBoxTestState { get; set; }
         /// <summary>
        /// 单元格边框颜色
        /// </summary>
        private Color CellBorderColor { get { return Color.FromArgb(172, 168, 153); } }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle

cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value,

object formattedValue, string errorText, DataGridViewCellStyle cellStyle,

DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            var check = (Boolean)value;
            CheckBoxTestState = check ? CheckBoxState.CheckedNormal :

CheckBoxState.UncheckedNormal;
            if (paintParts == DataGridViewPaintParts.Background || paintParts ==

DataGridViewPaintParts.All)
            {
                graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
            }
            if (paintParts == DataGridViewPaintParts.Border || paintParts ==

DataGridViewPaintParts.All)
            {
                graphics.DrawRectangle(new Pen(CellBorderColor), cellBounds);
            }
            if (paintParts == DataGridViewPaintParts.SelectionBackground || Selected)
            {
                graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor),

cellBounds);
            }
            Point checboxPoint = new Point(cellBounds.X + 4, cellBounds.Y + cellBounds.Height

/ 4);
            Rectangle checboxRegtangle = new Rectangle(new Point(cellBounds.X + 4,

cellBounds.Y + cellBounds.Height / 4), new Size(40, 40));
            Rectangle buttonRectangle = new Rectangle(new Point(cellBounds.X + 50,

cellBounds.Y + cellBounds.Height / 4), new Size(40, 20));
            ChecboxLocation = checboxRegtangle;
            ButtonLocation = buttonRectangle;
            CheckBoxRenderer.DrawCheckBox(graphics, checboxPoint,checboxRegtangle,"tasi",new

Font("宋体",12),false,CheckBoxTestState);
            ButtonRenderer.DrawButton(graphics, buttonRectangle, true,

PushButtonState.Default);
            // base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,

formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }

      //DataGridview标头单元格绘制两个按钮,并指定两个按钮的事件
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            //这里位置要计算好,计算出CheckBox位置
            if (e.Location.X > ChecboxLocation.Location.X && e.Location.X <

ChecboxLocation.Location.X + ChecboxLocation.Width)
            {
                if (checkboxClick != null)
                {
                    checkboxClick(e);
                }
            }
             //计算出button的位置
            else if(e.Location.X

>ButtonLocation.Location.X&&e.Location.X<ButtonLocation.Location.X + ButtonLocation.Width)
            {
                buttonClick(e);
            }
            base.OnMouseClick(e);
        }
    }
}



使用也很简单:(使用时先把结构加载完了在对这列进行如下操作)
    MyDataGridViewColumnHeaderCell mycell = new MyDataGridViewColumnHeaderCell();
            mycell.checkboxClick+=new MyDataGridViewColumnHeaderCell.CheckBoxClick

(mycell_checkboxClick);
            mycell.buttonClick += new MyDataGridViewColumnHeaderCell.ButtonClick

(mycell_buttonClick);
            mycell.Value = true;
            this.view.Columns[0].HeaderCell = mycell;
            view.CellPainting += new DataGridViewCellPaintingEventHandler

(dataGridView1_CellPainting);

这个里面我写了两个控件,有点注意的是点击事件时我对按钮的位置没有控制好,我暂时没有认真写这个,

要是用的话自己要想下如何控制点击事件的位置,第二个是按钮的大小我没有控制,这个也需要自己来控制

转载于:https://www.cnblogs.com/libinsoft/p/4201780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值