Check/Uncheck a checkbox on datagridview

前端 未结 16 732
野性不改
野性不改 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条回答
  •  心在旅途
    2020-11-30 11:40

    Here is another example you can try

    private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView.Columns["Select"].Index)
            {
                dataGridView.EndEdit();
    
                if ((bool)dataGridView.Rows[e.RowIndex].Cells["Select"].Value)
                {
                    //-- checking current select, needs to uncheck any other cells that are checked
                    foreach(DataGridViewRow row in dataGridView.Rows)
                    {
                        if (row.Index == e.RowIndex)
                        {
                            dataGridView.Rows[row.Index].SetValues(true);
                        }
                        else
                        {
                            dataGridView.Rows[row.Index].SetValues(false);
                        }
                    }
                }
            }
        }
    

提交回复
热议问题