How to Add Select All Checkbox in DataGrid view in WinForms?

Farshad Valizade 381 Reputation points

Hi every body.

my question is duplicate in the net and I have seen many topics about it but I haven't find my right solution.

I have a project with many datagridview in it. each form has one two or three datagridview and I want to have a select all checbox columns in all.

I don't code one by one is there any way or class to have to do this?

I have this code for put a checkbox column :

private void dgvCheckBox_CheckedChanged(object sender, EventArgs e)         
	foreach (DataGridViewRow r in DG_RequestInternal.Rows)             
		r.Cells[0].Value = ((CheckBox)sender).Checked;             
 private void DG_RequestInternal_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)         {             
	if (e.ColumnIndex == 0 && e.RowIndex == -1)             
	e.PaintBackground(e.ClipBounds, false);                 
	Point pt = e.CellBounds.Location;                  
	int nChkBoxWidth = 15;                 
	int nChkBoxHeight = 15;                 
	int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;                 
	int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;                  
	pt.X += offsetx;                 
	pt.Y += offsety;                  
	CheckBox cb = new CheckBox();                  
	cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);                 
	cb.Location = pt;                 
	cb.Checked = false;                 
	cb.CheckedChanged += new EventHandler(dgvCheckBox_CheckedChanged);
	e.Handled = true;              

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,229 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points

    See Toggling the States of all CheckBoxes Inside a DataGridView Column. I used it years ago and resurrected it as shown below. My code went from C# to VB.NET than back to C# found here.


    1 person found this answer helpful.

  2. KOZ6.0 4,735 Reputation points

    When using DataGridView, I choose to use DataSource property.

    1.Create a class that defines columns

    public class BindingItem
        [DisplayName(" ")]        public bool Checked { get; set; }
        [DisplayName("Editable")] public string EditableItem { get; set; }
        [DisplayName("Readonly")] public string ReadonlyItem { get; internal set; }
        [Browsable(false)]        public string HiddenItem1 { get; set; }
        public BindingItem() : this(string.Empty, string.Empty, string.Empty) { }
        public BindingItem(string editableItem, string readonlyItem,
                           string hiddenItem) {
            Checked = false;
            EditableItem = editableItem;
            ReadonlyItem = readonlyItem;
            HiddenItem1 = hiddenItem;
    DisplayName: Header Text
    internal set: Readonly Column
    Browsable(false):Item not displayed

    2.Create a BindingList and set it to the DataSource property

        var list = new BindingList<BindingItem> {
            new BindingItem("EditableItem1","ReadonlyItem1", "HiddenItem1"),
            new BindingItem("EditableItem2","ReadonlyItem2", "HiddenItem2"),
            new BindingItem("EditableItem3","ReadonlyItem3", "HiddenItem3"),
            new BindingItem("EditableItem4","ReadonlyItem4", "HiddenItem4"),
            new BindingItem("EditableItem5","ReadonlyItem5", "HiddenItem5"),
        dataGridView1.DataSource = list;

    3.replacing the header cell.

    You can use it just by replacing the header cell.

    dataGridView1.Columns[0].HeaderCell = 
       new DataGridViewSelectAllCheckBoxHeaderCell();