Click here to Skip to main content
15,888,301 members
Articles / Desktop Programming / Windows Forms
Article

CheckBox Header Column For DataGridView

Rate me:
Please Sign up or sign in to vote.
4.85/5 (33 votes)
23 Aug 20071 min read 292.4K   95   47
Presenting a solution for having CheckBox control in the header of DataGridView

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:

C#
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:

C#
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:

C#
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 CheckBoxes and do many others as with the normal CheckBox.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Austria Austria
Working as a .NET and database developer

Comments and Discussions

 
GeneralMy vote of 5 Pin
evry1falls14-Apr-24 16:47
evry1falls14-Apr-24 16:47 
QuestionText Alignment in Header Pin
Member 1575871719-Sep-22 0:52
Member 1575871719-Sep-22 0:52 
BugFirst Row is not getting updated. Pin
sejaltanna28-May-17 23:51
sejaltanna28-May-17 23:51 
QuestionUncheck box when a button is selected Pin
Member 1302199324-Feb-17 6:59
Member 1302199324-Feb-17 6:59 
QuestionTo the Author - Usage of this piece of code for Official purposes Pin
SharathNS4-Apr-16 21:57
SharathNS4-Apr-16 21:57 
SuggestionMy vote of 5 Pin
Kyaw Zin Soe13-Dec-15 23:11
Kyaw Zin Soe13-Dec-15 23:11 
QuestionUse Pin
aramosvizcarra28-Aug-14 3:26
aramosvizcarra28-Aug-14 3:26 
AnswerRe: Use Pin
DLM@TD25-Sep-14 8:14
DLM@TD25-Sep-14 8:14 
SuggestionVersion based on CheckBoxState: unchecked, checked, mixed and normal, hot, pressed Pin
dethtroll27-Jan-12 5:16
dethtroll27-Jan-12 5:16 
First class is an extension for enum of real checkbox states:
C#
public static class CheckBoxStateExtension
{
	public static bool IsExactUnchecked(this CheckBoxState state) {
		return (int)state > 0 && (int)state < 5;
	}
	public static bool IsExactChecked(this CheckBoxState state) {
		return (int)state > 4 && (int)state < 9;
	}
	public static bool IsExactMixed(this CheckBoxState state) {
		return (int)state > 8 && (int)state < 13;
	}

	public static bool IsDisabled(this CheckBoxState state) {
		return (int)state % 4 == 0;
	}
	public static bool IsNormal(this CheckBoxState state) {
		return (int)state % 4 == 1;
	}
	public static bool IsHot(this CheckBoxState state) {
		return (int)state % 4 == 2;
	}
	public static bool IsPressed(this CheckBoxState state) {
		return (int)state % 4 == 3;
	}

	public static CheckBoxState SetDisabled(this CheckBoxState state, bool value) {
		return SetModulo(state, value, 3, 0);
	}
	public static CheckBoxState SetNormal(this CheckBoxState state, bool value) {
		return SetModulo(state, value, 0, 0);
	}
	public static CheckBoxState SetHot(this CheckBoxState state, bool value) {
		return SetModulo(state, value, 1, 0);
	}
	public static CheckBoxState SetPressed(this CheckBoxState state, bool value) {
		return SetModulo(state, value, 2, 0);
	}

	public static CheckBoxState SetUnchecked(this CheckBoxState state, bool value) {
		return SetQuotient(state, value, 0, 1);
	}
	public static CheckBoxState SetChecked(this CheckBoxState state, bool value) {
		return SetQuotient(state, value, 1, 0);
	}
	public static CheckBoxState SetMixed(this CheckBoxState state, bool value) {
		return SetQuotient(state, value, 2, 2);
	}

	public static CheckBoxState SetModulo(this CheckBoxState state, bool value, int trueModulo, int falseModulo) {
		int safe = GetDurty(state);

		int valueQuotient = safe / 4;
		int valueMod = safe % 4;

		if (valueMod == trueModulo && value)
			return state;
		if (valueMod != trueModulo && !value)
			return state;

		return Restore(valueQuotient, value ? trueModulo : falseModulo);
	}
	public static CheckBoxState SetQuotient(this CheckBoxState state, bool value, int trueQuotient, int falseQuotient) {
		int safe = GetDurty(state);

		int valueQuotient = safe / 4;
		int valueMod = safe % 4;

		if (valueQuotient == trueQuotient && value)
			return state;
		if (valueQuotient != trueQuotient && !value)
			return state;

		return Restore(value ? trueQuotient : falseQuotient, valueMod);
	}

	private static int GetDurty(CheckBoxState value) {
		return (int)value - 1;
	}
	private static CheckBoxState Restore(int quotient, int mod) {
		return (CheckBoxState)(4 * quotient + mod + 1);
	}
}


Second class is clean version of DatagridViewCheckBoxHeaderCell:
C#
public class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
{
	private Rectangle boxBounds;
	private Point cellLocation;
	private bool headerCheckBoxClicked;
	private bool inited;

	public DatagridViewCheckBoxHeaderCell() {
		CheckBoxState = CheckBoxState.UncheckedNormal;
	}

	public event Action<bool> CheckBoxClicked;
	public void OnCheckBoxClicked(bool value) {
		if (CheckBoxClicked != null)
			CheckBoxClicked(value);
	}

	public CheckBoxState CheckBoxState {
		get;
		set;
	}
	public bool Checked {
		get { return CheckBoxState.IsExactChecked(); }
		set { CheckBoxState = CheckBoxState.SetChecked(value); }
	}

	protected override void Paint(Graphics graphics, Rectangle clipBounds, 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, null, null, errorText, cellStyle, advancedBorderStyle, paintParts);

		if (this.DataGridView != null && !inited)
			InitializeGridEvents();

		cellLocation = cellBounds.Location;

		var size = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal);
		boxBounds = new Rectangle(new Point
		{
			X = cellBounds.Location.X + (cellBounds.Width / 2) - (size.Width / 2),
			Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (size.Height / 2)
		}, size);

		CheckBoxRenderer.DrawCheckBox(graphics, boxBounds.Location, CheckBoxState);
	}

	protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) {
		if (IsBoxContains(e.X, e.Y))
			ToggleChecked();

		base.OnMouseClick(e);
	}
	protected override void OnMouseDoubleClick(DataGridViewCellMouseEventArgs e) {
		if (IsBoxContains(e.X, e.Y))
			ToggleChecked();

		base.OnMouseDoubleClick(e);
	}
	protected override void OnKeyUp(KeyEventArgs e, int rowIndex) {
		if (e.KeyCode == Keys.Space)
			ToggleChecked();

		base.OnKeyUp(e, rowIndex);
	}
	protected override void OnMouseMove(DataGridViewCellMouseEventArgs e) {
		MarkHot(IsBoxContains(e.X, e.Y));
		base.OnMouseMove(e);
	}
	protected override void OnMouseLeave(int rowIndex) {
		MarkHot(false);
		base.OnMouseLeave(rowIndex);
	}
	protected override void OnMouseDown(DataGridViewCellMouseEventArgs e) {
		MarkPressed(IsBoxContains(e.X, e.Y));
		base.OnMouseDown(e);
	}
	protected override void OnMouseUp(DataGridViewCellMouseEventArgs e) {
		MarkPressed(false);
		base.OnMouseUp(e);
	}

	private void InitializeGridEvents() {
		inited = true;
		this.DataGridView.CellValueChanged += GridCellValueChanged;
		this.DataGridView.CurrentCellDirtyStateChanged += GridCurrentCellDirtyStateChanged;
	}

	private void GridCellValueChanged(object sender, DataGridViewCellEventArgs e) {
		if (headerCheckBoxClicked)
			return;

		RowCheckboxClick(this.DataGridView[e.ColumnIndex, e.RowIndex]);
	}
	private void GridCurrentCellDirtyStateChanged(object sender, EventArgs e) {
		if (this.DataGridView.CurrentCell is DataGridViewCheckBoxCell)
			this.DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
	}
	private void RowCheckboxClick(DataGridViewCell checkBox) {
		if (checkBox == null)
			return;

		int count = this.DataGridView.Rows.Cast<DataGridViewRow>().Count(row => (bool)row.Cells[this.ColumnIndex].Value);
		if (count == 0)
			CheckBoxState = CheckBoxState.SetUnchecked(true);
		else if (count < this.DataGridView.RowCount)
			CheckBoxState = CheckBoxState.SetMixed(true);
		else
			CheckBoxState = CheckBoxState.SetChecked(true);
		this.DataGridView.InvalidateCell(this);
	}

	private bool IsBoxContains(int x, int y) {
		return (boxBounds.Contains(new Point(x + cellLocation.X, y + cellLocation.Y)));
	}

	private void ToggleChecked() {
		headerCheckBoxClicked = true;

		var @checked = !Checked;
		foreach (DataGridViewRow row in this.DataGridView.Rows)
			row.Cells[this.ColumnIndex].Value = @checked;
		this.DataGridView.RefreshEdit();

		OnCheckBoxClicked(Checked = @checked);
		this.DataGridView.InvalidateCell(this);

		headerCheckBoxClicked = false;
	}
	private void MarkHot(bool value) {
		CheckBoxState = CheckBoxState.SetHot(value);
		this.DataGridView.InvalidateCell(this);
	}
	private void MarkPressed(bool value) {
		CheckBoxState = CheckBoxState.SetPressed(value);
		this.DataGridView.InvalidateCell(this);
	}
}

And using
C#
this.CheckedColumn.HeaderCell = new DatagridViewCheckBoxHeaderCell();

Where CheckedColumn is DataGridViewCheckBoxColumn
Good luck
Questiona vb.net version Pin
jtxd30-Dec-11 15:32
jtxd30-Dec-11 15:32 
AnswerRe: a vb.net version Pin
Brent Murphy11-Jan-12 3:27
Brent Murphy11-Jan-12 3:27 
GeneralRe: a vb.net version Pin
tallion18-Nov-12 13:33
tallion18-Nov-12 13:33 
AnswerRe: a vb.net version Pin
evry1falls14-Apr-24 15:57
evry1falls14-Apr-24 15:57 
QuestionSetting 'DataPropertyName' stopped the OnCheckBoxClicked event firing Pin
trevormcalister17-Aug-11 4:31
trevormcalister17-Aug-11 4:31 
AnswerRe: Setting 'DataPropertyName' stopped the OnCheckBoxClicked event firing Pin
mohammad forutan31-Jul-12 21:15
mohammad forutan31-Jul-12 21:15 
GeneralThanks Pin
YZK30-Mar-11 0:03
YZK30-Mar-11 0:03 
GeneralMy vote of 5 Pin
JunfengGuo9-Mar-11 12:26
JunfengGuo9-Mar-11 12:26 
AnswerEven much simpler and a way to remove checkbox in an individual cell of in an CheckboxColumn Pin
loibl24-Mar-09 5:53
loibl24-Mar-09 5:53 
GeneralRe: Even much simpler and a way to remove checkbox in an individual cell of in an CheckboxColumn Pin
Christian Snook5-Nov-14 5:02
Christian Snook5-Nov-14 5:02 
QuestionHow to uncheck the header check box when user click on the grid cell and uncheck any of the check box Pin
ravikrmishra10-Feb-09 22:20
ravikrmishra10-Feb-09 22:20 
AnswerRe: Solution: to uncheck the header check box when user click on the grid cell and uncheck any of the check box Pin
Viji Raj1-Jul-09 10:30
Viji Raj1-Jul-09 10:30 
GeneralRe: Solution: to uncheck the header check box when user click on the grid cell and uncheck any of the check box Pin
Marpri22-Mar-12 0:55
Marpri22-Mar-12 0:55 
GeneralRe: Solution: to uncheck the header check box when user click on the grid cell and uncheck any of the check box Pin
mdimran2477-Jan-16 2:28
mdimran2477-Jan-16 2:28 
GeneralAppend check box after a column label Pin
TuttiFiesta30-Jan-09 3:48
TuttiFiesta30-Jan-09 3:48 
GeneralRe: Append check box after a column label Pin
TuttiFiesta30-Jan-09 4:23
TuttiFiesta30-Jan-09 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.