Comment cocher toutes les cases dans datagridview

la programmation


salut, j’ai écrit le code ci-dessous pour cocher toutes les cases mais je ne parviens pas à le faire. Aidez-moi s’il vous plaît

Ce que j’ai essayé :

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();
		}

Solution 1

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
}

Solution 2

Bonjour j’ai écrit ce code qui change tous des cases cochées à l’état opposé de la première case, dans mon cas, la case était sur la 3ème colonne de la datagridview.

C#
private void button1_Click(object sender, EventArgs e){
bool state= (bool)tblDocumentos.Rows[0].Cells[2].Value; //stores the state of the first row checkbox
    
foreach (DataGridViewRow row in mydatagridview.Rows)
    {
        row.Cells[2].Value = !state; //Sets the state of each checkbox to the opposite state of the first checkbox
    }
}

J’espère que cela aide

コメント

タイトルとURLをコピーしました