眾所周知,datagridview控件是CS架構(gòu)中用的比較頻繁的一個控件,里面提供了checkbox列的功能,可是卻沒有在列頭給出checkbox控件用于全選/全部取消所有行的功能,確實是個遺憾,這里就通過繪制實現(xiàn)這個功能.
效果圖:

一.創(chuàng)建一個表,里面包含bit字段,datagridview的DataGridViewCheckBoxColumn列會自動將其轉(zhuǎn)換成checkbox列
二. 界面上調(diào)用
DataGridView綁定完數(shù)據(jù)以后,在第一列繪制CheckBox列頭,可以在DataBindingComplete事件中處理
grid.DataBindingAfter += new Action(DataBindingAterEventHandler);

處理列頭全選事件

行多選處理

實現(xiàn)核心代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace YXSY.UI.UICommon
{
///
/// 功能描述:在DataGridView列頭繪制CheckBox控件
///
///
/// 創(chuàng)建日期:2010-12-03, 作者:Huang.Ting
///
//實現(xiàn)全選的事件參數(shù)類
public class DataGridviewCheckboxHeaderEventHander : EventArgs
{
private bool checkedState = false;
public bool CheckedState
{
get { return checkedState; }
set { checkedState = value; }
}
}
//與事件關(guān)聯(lián)的委托
public delegate void DataGridviewCheckboxHeaderCellEventHander(object sender, DataGridviewCheckboxHeaderEventHander e);
//重寫 DataGridViewColumnHeaderCell
public class DataGridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
private Point checkBoxLocation;
private Size checkBoxSize;
private bool isChecked = false;
private Point cellLocation = new Point();
private CheckBoxState cbState = CheckBoxState.UncheckedNormal;
public event DataGridviewCheckboxHeaderCellEventHander OnCheckBoxClicked;
//繪制列頭checkbox
protected override void Paint(Graphics g,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(g, clipBounds, cellBounds, rowIndex,dataGridViewElementState, value,formattedValue, errorText, cellStyle,advancedBorderStyle, paintParts);
Point p = new Point();
Size s = CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal);
//列頭checkbox的X坐標
p.X = cellBounds.Location.X +(cellBounds.Width / 2) - (s.Width / 2) - 1;
//列頭checkbox的Y坐標
p.Y = cellBounds.Location.Y +(cellBounds.Height / 2) - (s.Height / 2);
cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (isChecked)
cbState = CheckBoxState.CheckedNormal;
else
cbState =? CheckBoxState.UncheckedNormal;
//繪制復選框
CheckBoxRenderer.DrawCheckBox(g, checkBoxLocation, cbState);
}
///
/// 響應(yīng)點擊列頭checkbox單擊事件
///
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)
{
isChecked = !isChecked;
//獲取列頭checkbox的選擇狀態(tài)
DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander();
ex.CheckedState = isChecked;
//此處不代表選擇的列頭checkbox,只是作為參數(shù)傳遞。應(yīng)該列頭checkbox是繪制出來的,無法獲得它的實例
object sender = new object();
if (OnCheckBoxClicked != null)
{
//觸發(fā)單擊事件
OnCheckBoxClicked(sender, ex);
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
}