在項目的開發中,在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
轉載於:https://www.cnblogs.com/kevinGao/archive/2012/07/23/2776045.html