Stainn
Stainn

Reputation: 119

Unselect checkbox header if not all of checkbox in the gridview rows is checked

as the question says, I already can check the checkboxes in the gridview rows when the "Select All" CheckBox in the header is checked and uncheck the checkboxes in the gridview rows when the "Select All" CheckBox in the header is unchecked. And I want to do when not all of the checkboxes in the rows is checked, then the "Select All" CheckBox in the header is not checked, also vice versa (when all of the checkboxes in the rows is checked, then the "Select All" CheckBox in the header is checked).

How can I do that?

I already did like I want to achieve, but the checkbox in the header starts to affect (checked or unchecked) even I only check or uncheck a single checkbox in the rows, not all of them.

Here is the code that I am using:

protected void checkAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");

            if (ChkBoxHeader.Checked)
            {
                ChkBoxRows.Checked = true;
            }

            else
            {
                ChkBoxRows.Checked = false;
            }
        }
    }

protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkbx_select =  (CheckBox)sender;

        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (chkbx_select.Checked)
            {
                ChkBoxHeader.Checked = true;
            }

            else
            {
                ChkBoxHeader.Checked = false;
            }
        }
    }

Your answer much appreciated.

Thank you.

Upvotes: 0

Views: 2090

Answers (1)

Michael
Michael

Reputation: 3061

The idea is on every CheckedChanged event to iterate over all checkboxes (in all rows) and make sure all of them are checked. Something like this

protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
{    
        CheckBox chkbx_select =  (CheckBox)sender;
        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        if(!chkbx_select.Checked)
        {
           // Checkbox was unchecked, 
           // short circuit, set Select All Checkbox.Checked = false and return

           ChkBoxHeader.Checked = false;
           return;
        }

        bool allChecked = true;
        foreach (GridViewRow row in GridView1.Rows)
        {
            // this is pseudocode, find checkbox on each row
            var checkBox = row.FindControl("Checkbox") 

            if (!checkbox.Checked)
            {
                allChecked = false;
                break;
            }
        }

        ChkBoxHeader.Checked = allChecked;
  }

Upvotes: 1

Related Questions