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

时间:2022-03-07 04:04:44
在国外的源码网站看到一个带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);
            }
        }

    }

}