none
How to uncheck the header check box when user click on the datagridview cell and uncheck any of the checkbox RRS feed

  • Question

  • Hi all,

    I created a CheckBoxColumn in my DataGridView control as per below code:

    DataGridViewCheckBoxColumn checkBoxCol = new DataGridViewCheckBoxColumn();
    clsDataGridViewCheckBoxHeaderCell checkBoxHeader = new clsDataGridViewCheckBoxHeaderCell();
    checkBoxCol.HeaderCell = checkBoxHeader;
    checkBoxCol.Width = 50;
    dgv_1.Columns.Add(checkBoxCol);
    checkBoxHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(CheckBoxHeader_OnCheckBoxClicked);

    And I am able to check and uncheck all the checkboxcells in my DataGridView by below code:

    private void CheckBoxHeader_OnCheckBoxClicked(bool isCheckedAll)
    {
    	try
    	{
    		dgv_1.EndEdit();
    		foreach (DataGridViewRow row in dgv_1.Rows)
    		{
    			row.Cells[0].Value = isCheckedAll;
    		}
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.Message, _CURRENT_FORM_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
    	}
    }

    While i am using the DatagridViewCheckBoxHeaderCell class created as per this link: http://www.codeproject.com/Articles/20165/CheckBox-Header-Column-For-DataGridView

    but I am having trouble to:

    1. Untick the header check box when user unchcecks any of the checkboxcell in my datagridview.

    2. when all the checkboxes ticked in datagridview, the header check box should be ticked.

    Appreciate your help in advance.

    Tuesday, June 4, 2019 5:39 AM

Answers

  • Hello,

    I recommend the following 

    https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AndieDu Wednesday, June 12, 2019 4:05 AM
    Wednesday, June 5, 2019 12:31 PM
    Moderator
  • Hi all,

    After some time of exploring, I have no choice but adopt the code in codeproject as per this link: https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat

    And I had to modify this piece of code in this method to fix the header check box tick and untick issue:

    private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)

    basically i am counting the TotalCheckedCheckBoxes that have been ticked and compare with the total rows in DataGridView, if TotalCheckedCheckBoxes == TotalCheckBoxes then tick the HeaderCheckBox, otherwise untick, and below is the code of that method:

    private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)
    {
    	if (RCheckBox != null)
    	{
    		TotalCheckedCheckBoxes = 0;
    		//Modifiy Counter;            
    		//if ((bool)RCheckBox.Value && TotalCheckedCheckBoxes < TotalCheckBoxes)
    		//    TotalCheckedCheckBoxes++;
    		//else if (TotalCheckedCheckBoxes > 0)
    		//    TotalCheckedCheckBoxes--;
    
    		foreach (DataGridViewRow dr in dgv.Rows)
    		{
    			if (Convert.ToBoolean(dr.Cells[0].Value) == true)
    			{
    				TotalCheckedCheckBoxes++;
    			}
    		}
    		//Change state of the header CheckBox.
    		if (TotalCheckedCheckBoxes < TotalCheckBoxes)
    			HeaderCheckBox.Checked = false;
    		else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
    			HeaderCheckBox.Checked = true;
    	}
    }
    • Marked as answer by AndieDu Wednesday, June 12, 2019 4:05 AM
    Wednesday, June 12, 2019 4:05 AM

All replies

  • anyone had such issue and got it fixed??
    Tuesday, June 4, 2019 10:20 PM
  • Hi AndieDu,

    I think adding the control "CheckBox" into DataGridView directly is easier.

        CheckBox checkBox1 = new CheckBox();
    
        private void Form1_Load(object sender, EventArgs e)
        {
            checkBox1.Size = new System.Drawing.Size(15, 15);
            checkBox1.BackColor = Color.Transparent;
            checkBox1.Padding = new Padding(0);
            checkBox1.Margin = new Padding(0);
            dataGridView1.Columns[0].HeaderText = "       Selected";
            checkBox1.Parent = dataGridView1;
            DataGridViewHeaderCell header = dataGridView1.Columns[0].HeaderCell;
            checkBox1.Location = new Point(
                header.ContentBounds.Left + dataGridView1.Columns[0].Width / 2,
                header.ContentBounds.Top
            );
            checkBox1.CheckedChanged += CheckBox1_CheckedChanged;
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 5, 2019 6:03 AM
    Moderator
  • Hello,

    I recommend the following 

    https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AndieDu Wednesday, June 12, 2019 4:05 AM
    Wednesday, June 5, 2019 12:31 PM
    Moderator
  • thanks Kyle, but your code wont be fulfilling what i was planned.
    Thursday, June 6, 2019 4:59 AM
  • thanks, but i think it is over killing and there is must a simple way to implement my request.
    Thursday, June 6, 2019 5:00 AM
  • Then try this

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private DataGridViewCheckBoxColumn _headerBoxColumn;
            private CheckBox _checkBox;
            
            void Header_CheckedChanged(object sender, EventArgs e)
            {
                for (int rowIndex = 0; rowIndex < dataGridView1.RowCount; rowIndex++)
                {
                    dataGridView1[0, rowIndex].Value = _checkBox.Checked;
                }
                dataGridView1.EndEdit();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                _headerBoxColumn = new DataGridViewCheckBoxColumn {Name = "selection"};
                _headerBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    
                dataGridView1.Columns.Add(_headerBoxColumn);
    
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
    
                _checkBox = new CheckBox();
                var rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
                _checkBox.Size = new Size(18, 18);
                _checkBox.Location = rect.Location;
                _checkBox.CheckedChanged += Header_CheckedChanged;
                dataGridView1.Controls.Add(_checkBox);
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 7, 2019 1:21 AM
    Moderator
  • Hi all,

    After some time of exploring, I have no choice but adopt the code in codeproject as per this link: https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat

    And I had to modify this piece of code in this method to fix the header check box tick and untick issue:

    private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)

    basically i am counting the TotalCheckedCheckBoxes that have been ticked and compare with the total rows in DataGridView, if TotalCheckedCheckBoxes == TotalCheckBoxes then tick the HeaderCheckBox, otherwise untick, and below is the code of that method:

    private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)
    {
    	if (RCheckBox != null)
    	{
    		TotalCheckedCheckBoxes = 0;
    		//Modifiy Counter;            
    		//if ((bool)RCheckBox.Value && TotalCheckedCheckBoxes < TotalCheckBoxes)
    		//    TotalCheckedCheckBoxes++;
    		//else if (TotalCheckedCheckBoxes > 0)
    		//    TotalCheckedCheckBoxes--;
    
    		foreach (DataGridViewRow dr in dgv.Rows)
    		{
    			if (Convert.ToBoolean(dr.Cells[0].Value) == true)
    			{
    				TotalCheckedCheckBoxes++;
    			}
    		}
    		//Change state of the header CheckBox.
    		if (TotalCheckedCheckBoxes < TotalCheckBoxes)
    			HeaderCheckBox.Checked = false;
    		else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
    			HeaderCheckBox.Checked = true;
    	}
    }
    • Marked as answer by AndieDu Wednesday, June 12, 2019 4:05 AM
    Wednesday, June 12, 2019 4:05 AM