Cara memilih semua kotak centang di datagridview

pemrograman


hai saya telah menulis kode di bawah ini untuk memilih semua kotak centang tetapi saya tidak dapat melakukannya. mohon bantuannya

Apa yang saya coba:

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

Solusi 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
}

Solusi 2

Halo, saya menulis kode ini yang berubah semua dari kotak yang dicentang ke keadaan kebalikan dari kotak centang pertama, dalam kasus saya kotak centang berada di kolom ke-3 dari 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
    }
}

Semoga ini bisa membantu

コメント

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