How to verify if a DataGridViewCheckBoxCell is Checked

前端 未结 7 1677
小鲜肉
小鲜肉 2021-01-11 10:45

I have bound a data table to a DataGridView, this data table has a column called \"Status\" which is of type Boolean. I can set the value to

7条回答
  •  醉话见心
    2021-01-11 11:38

    Thank you all. Had the same problem but i find out that writing senderGrid.EndEdit(), before checking the value, resolves it.

    private void dgvRiscos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            senderGrid.EndEdit();
    
            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
                e.RowIndex >= 0)
            {
    
                var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[e.RowIndex].Cells["associado"];
                if ((bool)cbxCell.Value)
                {
                       // Criar registo na base de dados
                }
                else
                {
                       // Remover registo da base de dados
                }
            }
        }
    

    Keep up the good work

提交回复
热议问题