Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have written belw code to select all checkboxes but i am unable to do so.please help

What I have tried:

CheckBox HeaderCheckBox=null;
    bool IsHeaderCheckBoxClicked=false;
    private void AddHeaderCheckBox()
    {
        HeaderCheckBox= new CheckBox();
        HeaderCheckBox.Size=new Size(15,15);
        //add check ti DGV
        this.dataGridView1.Controls.Add(HeaderCheckBox);
    }
    private void HeaderCheckBoxClick(CheckBox HCheckBox)
    {
        IsHeaderCheckBoxClicked =true;
        foreach(DataGridView Row in dataGridView1.Rows)
            (DataGridViewCheckBoxCell)Row.CellStateChanged["chk"].value=HCheckBox.Checked;
    }

public void MainFormLoad(object sender, EventArgs e)
		{	

AddHeaderCheckBox();
		}
Posted
Updated 25-May-18 4:42am
Comments
[no name] 25-May-18 8:49am    
What issue you are facing?

1 solution

foreach (DataGridViewRow row in dataGridView1.Rows)
{
       DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
       chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}
 
Share this answer
 
Comments
Member 12505620 5-Jun-23 7:16am    
Is it possible to clear the checked values when I unchecked the header checkbox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900