private void Form1_Load(object sender, EventArgs e)
{
object[][] dbData = new object[][]
{new object[]{1,null,null}
,new object[]{2,null,null}
,new object[]{3,null,null}
,new object[]{4,null,null}
,new object[]{5,null,null}};
foreach (object[] obj in dbData)
dgv.Rows.Add(obj);
//第一組checkbox
CheckBoxSelectAll.DatagridViewCheckBoxHeaderCell cbHeader1 = new CheckBoxSelectAll.DatagridViewCheckBoxHeaderCell();
Column2.HeaderCell = cbHeader1;
Column2.HeaderText = string.Empty;
cbHeader1.OnCheckBoxClicked += new CheckBoxSelectAll.CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
//第二組checkbox
CheckBoxSelectAll.DatagridViewCheckBoxHeaderCell cbHeader2 = new CheckBoxSelectAll.DatagridViewCheckBoxHeaderCell();
Column3.HeaderCell = cbHeader2;
Column3.HeaderText = string.Empty;
cbHeader2.OnCheckBoxClicked += new CheckBoxSelectAll.CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
}
//DataGridView全選功能
public void cbHeader_OnCheckBoxClicked(bool b, DataGridViewCellMouseEventArgs e)
{
bool value = false;
if (b)
value = b;
foreach (DataGridViewRow row in dgv.Rows)
row.Cells[e.ColumnIndex].Value = value;
}
//DataGridView全選按鈕
public class CheckBoxSelectAll
{
public delegate void CheckBoxClickedHandler(bool state, DataGridViewCellMouseEventArgs e);
public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
{
bool _bChecked;
int _columnIndex;
public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked, DataGridViewCellMouseEventArgs e)
{
_bChecked = bChecked;
_columnIndex = e.ColumnIndex;
}
public bool Checked
{
get { return _bChecked; }
}
public int ColumnIndex
{
get { return _columnIndex; }
}
}
public 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, e);
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
}