原创 c# 封装的带CheckBox的DataGridViewColumnHeaderCell 源码部分 实现DataGridView列头带CheckBox控件实现全选功能,支持列头带标题

摘要:
它的实现方法是直接在单元格中绘制复选框。在本例中,我在单元格中创建一个CheckBox对象,将CheckBox控件加载到DataGridView容器中,最后用这个CheckBox覆盖单元格。目前,将首先发布源代码,然后在未来封装相应的DataGridViewColumn类。最后,为了您的方便,它将被打包到一个dll中。此外,还提供了一个dataGridview来在列标题中添加一个组合框以实现过滤功能。使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Drawing;使用System.Data;使用System.Text;使用System.Windows.Forms;使用System.Diagnostics;/**名称:DataGridViewColumnHeaderCell with CheckBox**作者:www.cnblogs.com/edzjx**时间:2009年11月29日**备注:Cellhead被CheckBox替换。使用的方法是复选框**声明:可以联系作者以了解不足之处。任何人都可以使用此源代码来尊重作者的作品。请保留作者的信息。
在国外的源码网站看到一个带CheckBox的源码的。可惜的是,他不支持
列头显示文字,而且全选功能是需要自己写好函数手动添加到他的事件里面。
它实现的办法是在cell直接绘制一个CheckBox框。而我这个是在cell里创建一个CheckBox对象,把Checkbox控件加载到DataGridView容器中,最后用这个CheckBox覆盖这个Cell。
目前先释出源码,以后会再封装一个与之适应的DataGridViewColumn类,最后打包成dll,共大家方便调用。
另外提供一个dataGridview在列头加combobox实现筛选功能的组件。包括源码和实例。http://www.microsoft.com/downloads/details.aspx?FamilyID=bea5c31b-e07a-46b5-8662-ecd798c0134d&displaylang=en
PS:我说的人家写的类头带Checkbox的园子里已经有人介绍大家可以是搜搜。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
/*
 * 名称:带CheckBox的DataGridViewColumnHeaderCell
 *
 * 作者:www.cnblogs.com/edzjx
 * 
 * 时间:2009-11-29
 * 
 * 备注:cellhead用Checkbox来代替,采用的方法是checkbox
 *  
 * 申明:不足之处可以跟作者联系,任何人可以使用此源码,出于尊重作者的劳动,请保留作者信息。
 */
namespace DataGridViewCheckBoxColumnHeeader
{
    //定义触发单击事件的委托
    public delegate void datagridviewcheckboxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);


    //定义包含列头checkbox选择状态的参数类
    class datagridviewCheckboxHeaderEventArgs : EventArgs
    {
        private bool checkedState = false;

        public bool CheckedState
        {
            get { return checkedState; }
            set { checkedState = value; }
        }
    }



    //定义继承于DataGridViewColumnHeaderCell的类,用于绘制checkbox,定义checkbox鼠标单击事件
    public class DataGridViewCheckBoxColumnHeeaderCell : DataGridViewColumnHeaderCell
    {

        static int counts = 0;

        Point _cellLocation = new Point();



        public DataGridViewCheckBoxColumnHeeaderCell()
        {
           counts++;
           ch.CheckedChanged += new EventHandler(ch_CheckedChanged);
           ch.MouseClick += new MouseEventHandler(ch_MouseClick);
        }

        void ch_MouseClick(object sender, MouseEventArgs e)
        {
           DataGridViewCellMouseEventArgs ex = new DataGridViewCellMouseEventArgs(this.ColumnIndex, -1, this._cellLocation.X, this._cellLocation.Y, e);

           base.OnMouseClick(ex);

        }

        void ch_CheckedChanged(object sender, EventArgs e)
        {
            if (this.EnableSelectAll)
            {
                for (int i = 0; i < this.DataGridView.Rows.Count; i++)
                {
                    
                    this.DataGridView[this.ColumnIndex, i].Value = this.ch.Checked;
                }
            }
        }

        bool _EnableSelectAll = true;
        /// <summary>
        /// Gets or sets EnableSelectAll
        /// </summary>
        [DefaultValue(true)]
        public bool EnableSelectAll
        {
            get
            {

                return _EnableSelectAll;

            }
            set
            {

                _EnableSelectAll = value;
            }
        }


          System.Windows.Forms.CheckBox ch = new System.Windows.Forms.CheckBox();
        /// <summary>
        /// Gets or sets this's checkbox.
        /// </summary>
        [DefaultValue(true)]
        public System.Windows.Forms.CheckBox CheckBox
        {
            get
            {

                return ch;
                
            }
            set
            {

                ch = value;
            }
        }


        //绘制列头checkbox
        protected override void Paint(System.Drawing.Graphics graphics,
           System.Drawing.Rectangle clipBounds,
           System.Drawing.Rectangle cellBounds,
           int rowIndex,
           DataGridViewElementStates dataGridViewElementState,
           object value,
           object formattedValue,
           string errorText,
           DataGridViewCellStyle cellStyle,
           DataGridViewAdvancedBorderStyle advancedBorderStyle,
           DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                dataGridViewElementState, value,
                formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);
           
            //checkbox的方位配置。
            Rectangle checkbounds=new Rectangle();
            checkbounds.X = cellBounds.X + 1; checkbounds.Y = cellBounds.Y + 2;
            checkbounds.Height = cellBounds.Height - 3;
            if ((cellBounds.Width + cellBounds.X) > this.DataGridView.Width)
            {
                checkbounds.Width = this.DataGridView.Width - checkbounds.X - 10;
            }
            else
            {
                checkbounds.Width = cellBounds.Width - 10;
            }

            _cellLocation = cellBounds.Location;
            
            
            
            Debug.WriteLine(counts.ToString());

            //配置Check
            ch.Name = "Chbox" + counts.ToString();
            ch.Text = this.Value.ToString();
            ch.FlatStyle = System.Windows.Forms.FlatStyle.System;
            ch.UseVisualStyleBackColor = true;
            ch.Margin = new System.Windows.Forms.Padding(0);
            ch.TextAlign = ContentAlignment.TopLeft;
            ch.Bounds = checkbounds;

            this.DataGridView.Controls.Add(ch);

            Debug.WriteLine(this.DataGridView.Controls.Count + " " + this.DataGridView.Controls[ch.Name]);
            ch.BringToFront();
            
            this.DataGridView.CellValueChanged += new DataGridViewCellEventHandler(DataGridView_CellValueChanged);
        }

        //对于单元格单元格内容改变时,需要重绘checkbox。
        void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == this.ColumnIndex && e.RowIndex == -1)
            {
                this.CheckBox.Text = this.Value.ToString();
                this.CheckBox.BringToFront();
                ((DataGridView)sender).InvalidateCell(this);
            }
            if (e.ColumnIndex == -1 && e.RowIndex == -1)
            {
                ((DataGridView)sender).InvalidateCell(this);
            }
        }

    }

}

免责声明:文章转载自《原创 c# 封装的带CheckBox的DataGridViewColumnHeaderCell 源码部分 实现DataGridView列头带CheckBox控件实现全选功能,支持列头带标题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux定时重启节约内存绕口令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

centos7源码安装cloud-init

<template> <name>centos72-source</name> <os> <name>CentOS-7</name> <version>2</version> <arch>x86_64</arc...

第八章 Python 对象和类

一、什么是对象 在 Pyth 中,对象就是经过实例化的,具体可以操作的一组代码的组合; 对象一般包含数据(变量,更习惯称之为属性 attribute),也包含代码(函数,也称之为方法) 当你想要创建一个别人从来都没有创建过的新对象时,首先必须定义一个类,用以指明该类型的对象所包含的内容(属性和方法) 可以把对象想象成 名词 ,那么方法就是动词。对象代表着一...

cJSON库源码分析

本文采用以下协议进行授权:自由转载-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。             cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的Json格式解析库。          那什么是Json格式?这里照搬度娘百科的说法:         ...

如何调试没有源码的.Net程序

在.Net开发过程中,经常会使用一些没有源码的第三方库,在代码出了问题时,如果怀疑跟该库的内部实现有关,我们该怎么办呢?首先,自然会想到反编译去看看代码或者联系作者,然而,有没有办法让我们在debug时进入这个第三方库,并看看里面在运行时到底发生了什么呢?本文就来介绍三种debug第三方库的办法,希望能够对你有所帮助。 先介绍一下我们的样例代码,下面这段代...

如何优雅的在java中统计代码块耗时

如何优雅的在java中统计代码块耗时 在我们的实际开发中,多多少少会遇到统计一段代码片段的耗时的情况,我们一般的写法如下 long start = System.currentTimeMillis(); try { // .... 具体的代码段 } finally { System.out.println("cost: " + (Syste...

HTML页面如何判断是手机访问还是电脑访问

可以通过js来判断访问设备,代码如下: 1 <script type="text/javascript"> 2 var system ={}; 3 var p = navigator.platform; 4 system.win = p.indexOf("Win") == 0;...