winform 中自定义DataGridView的批量选择列,实现点击列头的选择框,实现row中的批量选中或者取消选中。
public class DatagridviewCheckboxHeaderCell :DataGridViewColumnHeaderCell
{
public delegate void HeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);
public event HeaderEventHander OnCheckBoxClicked;
Point checkBoxLocation;
Size checkBoxSize;
public bool _checked = false;
Point _cellLocation = new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
//绘制列头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);
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) - 1;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y +
(cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标
_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);
}
// /// <summary>
// 点击列头checkbox单击事件
// </summary>
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;
//获取列头checkbox的选择状态
datagridviewCheckboxHeaderEventArgs ex = new datagridviewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender = this.DataGridView;//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
this.DataGridView.EndEdit();
}
}
public class datagridviewCheckboxHeaderEventArgs : EventArgs
{
private bool checkedState = false;
public bool CheckedState
{
get { return checkedState; }
set { checkedState = value; }
}
}
使用的代码,创建DataGridViewCheckBoxColumn把HeaderCell赋值DatagridviewCheckboxHeaderCell类型。
var checkBoxColumn = new DataGridViewCheckBoxColumn
{
AutoSizeMode = DataGridViewAutoSizeColumnMode.None,
Width = 50,
Name = "",
HeaderText = "",
SortMode = DataGridViewColumnSortMode.NotSortable
};
var ch= new DatagridviewCheckboxHeaderCell();
ch.OnCheckBoxClicked += Ch_OnCheckBoxClicked;
checkBoxColumn.HeaderCell = ch;
事件中的代码
private void OnCheckBoxClicked(object sender, datagridviewCheckboxHeaderEventArgs e)
{
var dataGridView = sender as DataGridView;
foreach (DataGridViewRow dgvRow in dataGridView.Rows)
{
if (e.CheckedState)
{
dgvRow.Cells[0].Value = true;
}
else
{
dgvRow.Cells[0].Value = false;
}
}
}