Introduction
This article will show you how to create CheckBoxHeaderColumn
for DataGridView
. It will expose CheckBoxClicked
event which you can consume in your client application.
Background
It is very common to have a list of items in DataGridView
with a check box in the first column where your later action will depend on user selection. This can be very easily done by having a first column defined as DataGridViewCheckBoxCell
object. But, how can your customer select all items in the list (let's say you are working on an email client app and the user wants to delete all of his 100 spams). This was the main idea to generate a class which will have a check box item in the header where the developer can have full control after the user checks/unchecks an item in the header. A common action is to check/uncheck all items in the DataGridView
depending on whether the header is checked/unchecked.
Using the Code
The whole solution (control) is very simple and contains just one class DataGridViewCheckBoxHeaderCell
.
On the client side, the developer just needs to define DataGridViewCheckBoxColumn
and assign DataGridCheckBoxHeaderCell
as a HeaderCell
. Here is the code to do that:
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
colCB.HeaderCell = cbHeader;
datagridview1.Columns.Add(colCB);
As mentioned before, always user clicks on a Checkbox
in the header object will fire CheckboxClicked
event which you can consume in your application:
cbHeader.OnCheckBoxClicked +=
new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
In the function cbHeader_OnCheckBoxClicked()
, you can check/uncheck all of DataGridView
rows or do any other action.
And here is a control source:
namespace TestRef
{
public delegate void CheckBoxClickedHandler(bool state);
public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
{
bool _bChecked;
public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
{
_bChecked = bChecked;
}
public bool Checked
{
get { return _bChecked; }
}
}
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);
}
}
}
I hope you will find this article helpful while this is a solution used in our cross database comparison tool for selecting rows for synchronization between databases. Besides this, you can easily extend and fully customize painting of the CheckBox
control, introduce tri state CheckBox
es and do many others as with the normal CheckBox
.