Check/Uncheck a checkbox on datagridview

前端 未结 16 736
野性不改
野性不改 2020-11-30 11:19

Can someone help me why it doesn\'t work? I have a checkbox and if I click on it, this should uncheck all the checkbox inside the datagridview which were check

16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 11:45

    Try the below code it should work

    private void checkBox2_CheckedChanged(object sender, EventArgs e) 
    {
        if (checkBox2.Checked == false)
        {
            foreach (DataGridViewRow row in dGV1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.Value = chk.TrueValue;
            }
        }
       else if (checkBox2.Checked == true)
        {
            foreach (DataGridViewRow row in dGV1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.Value = 1;
                if (row.IsNewRow)
                {
                    chk.Value = 0;
                }
            }
        }
    }
    

提交回复
热议问题