在项目的开发中,在DataGridView中将CheckBox作为第一列使用的很平常,使用微软自带DataGridView中的DataGridViewCheckBoxCell,但是微软自带的DataGridView中又没有能够将CheckBox作为列头来做全选和全取消选择的功能。所以如果想实现在列头上显示一个CheckBox并且点击CheckBox来实现全选和全取消,就没有现成的。但是办法是人想出来的,既然微软没有能够提供现成的实现方法,那我们就要自己动手,才能丰衣足食了。其实这个功能实现起来也不是很难,我们首先要定义一个DatagridViewCheckBoxHeaderCell类,它是继承自DataGridViewColumnHeaderCell,主要是要重写里面的OnPaint方法和OnMouseClick方法即可,代码如下:

 class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
    {
        Point checkBoxLocation;
        Size checkBoxSize;
        bool _checked = false;
        Point _cellLocation = new Point();
        System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
        public event CheckBoxClickedHandler OnCheckBoxClicked;

        public DatagridViewCheckBoxHeaderCell()
        {
        }

        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);
            Point p = new Point();
            Size s = CheckBoxRenderer.GetGlyphSize(graphics,
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            p.X = cellBounds.Location.X +
                (cellBounds.Width / 2) - (s.Width / 2);
            p.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (s.Height / 2);
            _cellLocation = cellBounds.Location;
            checkBoxLocation = p;
            checkBoxSize = s;
            if (_checked)
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.CheckedNormal;
            else
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.UncheckedNormal;
            CheckBoxRenderer.DrawCheckBox
            (graphics, checkBoxLocation, _cbState);
        }

        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
            if (p.X >= checkBoxLocation.X && p.X <=
                checkBoxLocation.X + checkBoxSize.Width
            && p.Y >= checkBoxLocation.Y && p.Y <=
                checkBoxLocation.Y + checkBoxSize.Height)
            {
                _checked = !_checked;
                if (OnCheckBoxClicked != null)
                {
                    OnCheckBoxClicked(_checked);
                    this.DataGridView.InvalidateCell(this);
                }

            }
            base.OnMouseClick(e);
        }    

    }

 

除此之外,还需要加上事件处理的委托,代码如下:

 public delegate void CheckBoxClickedHandler(bool state);
    public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
    {
        bool _bChecked;
        public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
        {
            _bChecked = bChecked;
        }
        public bool Checked
        {
            get { return _bChecked; }
        }
    }

然后在客户端加上如下代码:

DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();DatagridViewCheckBoxHeaderCell cbHeader =new DatagridViewCheckBoxHeaderCell();colCB.HeaderCell = cbHeader;datagridview1.Columns.Add(colCB);

再在客户端完成事件处理:

cbHeader.OnCheckBoxClicked +=     new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);

主要的代码实现就这么多呢。

为了方便大家的使用,我做了一个Demo来演示具体怎么使用,可以到我的资源中去下载:http://download.csdn.net/detail/weizhiai12/4445491

原文的作者没有提供例子。

想查看原文的可以点击如下地址查看:

http://www.codeproject.com/Articles/20165/CheckBox-Header-Column-For-DataGridView