最近突然想在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);

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

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

重绘DataGridView标头的更多相关文章

  1. C# 自定义重绘DataGridView

    using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...

  2. 重绘DataGridView的DataGridViewCheckBoxCell控件

    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...

  3. 『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)

    原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243 最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东 ...

  4. C# DataGridView 更改类型 重绘

    DataGridView   更改类型 需要用到重绘 DataGridViewTextBoxColumn aa01 = new DataGridViewTextBoxColumn(); aa00.Da ...

  5. 关于DOM的操作以及性能优化问题-重绘重排

     写在前面: 大家都知道DOM的操作很昂贵. 然后贵在什么地方呢? 一.访问DOM元素 二.修改DOM引起的重绘重排 一.访问DOM 像书上的比喻:把DOM和JavaScript(这里指ECMScri ...

  6. 关于repaint(重绘)和reflow( 回流)

    repaint就是重绘,reflow就是回流.repaint主要是针对某一个DOM元素进行的重绘,reflow则是回流,针对整个页面的重排 严重性: 在性能优先的前提下,性能消耗 reflow大于re ...

  7. MFC 滑动条的重绘

    MFC自带的滑动条的样子是这样的. 比较难看,所以需要重绘下,重绘后的样子是这样的. 代码如下: CustomSliderCtr.h #pragma once // CCustomSliderCtr ...

  8. WinForm中重绘TabControl选项卡标题

    最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...

  9. 回流(reflow)与重绘(repaint)

    最近项目排期不紧,于是看了一下之前看了好久也没看明白的chrome调试工具的timeline.但是很遗憾,虽然大概懂了每一项是做什么的,但是用起来并不能得心应手.所以今天的重点不是timeline,而 ...

随机推荐

  1. 单例模式解决RabbitMQ超出最大连接问题

    今天在项目稳定性测试过程中遇到一个情景:通过工具jMeter一直请求消息转发服务器,消息转发服务器再向rabbitMQ发送数据,在这期间出现了问题.MQ意外宕机. 1. 查看rabbitMQ管理界面. ...

  2. 记一次 Hibernate 插入数据中文乱码报错解决

    错误描述 程序运行,向表中插入数据(包含中文)报错:\xE6\xB2\x88\xE9\x9B\xAA... 但是自己另外新建一个数据库手动插入数据中文正常,同样修改数据库,表的编码之后同样不行.而且 ...

  3. canvas裁剪之后的base64转换为上传文件blob对象

    function convertBase64UrlToBlob(urlData){ var bytes=window.atob(urlData.split(',')[1]); //去掉url的头,并转 ...

  4. 【转】keyCode对照表及JS监听组合按键

    原文: http://blog.csdn.net/qq_21386275/article/details/67640576 有一些需求,html 页面上的input 框只允许输入数字,  只允许输入小 ...

  5. 怎样用EA设计ER图

    我们开发系统从文档開始,而EA就是替我们开发文档的好工具.结束了我们从概念设计到逻辑设计中的非常多问题.完好我们的文档. 如今就给大家说说如何在EA中设计概念模型ER图: 首先打开EA-"新 ...

  6. Eclipse导入外部项目问题总结

     此次在项目开发过程中导入从oksvn下载的共享项目时出现几个项目在不同的IDE导入导出时的问题,为免忘记做例如以下笔记: 1 类路径问题 在Java开发中大多数的开发人员使用的IDE是MyEcl ...

  7. Mongo使用

    在用mongo进行查询时,$exists表示是否document是否包括这个field,即使field的value为null也算是包括. $exists Syntax: { field: { $exi ...

  8. Android横竖屏切换不重新调用onCreate()

    再次感叹Android的碎片化!!!! 设置AndroidManifest.xml中Activity的android:configChanges=”keyboardHidden|orientation ...

  9. PCB javascript解析钻孔(Excellon)格式实现方法

    解析钻孔(Excellon)格式前首先得了解此格式,这样才能更好的解析呀. 一个钻孔里面包含的基本信息如下: 1.单位:公式mm,英制inch 2.省零方式:前省零,后省零 3.坐标方式:绝对坐标,相 ...

  10. 洛谷P1182数列分段

    题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 4 5 1要分成3段 将其如下分段: [4 ...