dimanche 20 février 2022

How to exclusively set the value of a DataGridViewCheckBoxCell?

I have a List<Car> objects that have a bool property named Marked.
I want to check / uncheck the Cell corresponding to this property, in a way that only one Car can be selected at the same time.
I want that the value of this property is updated in the bank.

Sample table:

Car Name Marked
a  
b  

The problem is I can't check the state of CheckBox Cells.
This is what I tried, but it's not working:

Example 1:

DataGridViewCheckBoxCell dataGridViewCheckBoxCell = DataGridView1.Rows[e.RowIndex].Cells["Marked"] as DataGridViewCheckBoxCell;

if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == true)
{
        //some code
}
else if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == false)
{
        //some code
}

Example 2:

foreach (DataGridViewRow row in DataGridView1.Rows)
{
    DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells["Marked"];

    if (chk.Value == chk.TrueValue)
    {
        chk.Value = chk.FalseValue;
    }
    else
    {
        chk.Value = chk.TrueValue;
    }
}

how can I do it?




Aucun commentaire:

Enregistrer un commentaire