原创 c# 封装的带CheckBox的DataGridViewColumnHeaderCell 源码部分 实现DataGridView列头带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);
            }
        }

    }

}

转载于:https://www.cnblogs.com/edzjx/archive/2009/11/29/1613200.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值