samedi 19 février 2022

How To Set DataGridViewCheckBoxColumn/Cell

I have a list of car objects that have a property called "Marked" that it's type is boolean. I want to check / uncheck this property, select only one of them, ie only one car can be selected from this list and at the same time I want the value true or The false of this property should be update in the bank.

exmple table:

Car Name Marked
a  
b  

problem is i can't check state of datagridviewcheckboxcolumn/cell.for example i try blow codes but they don't working.

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

exmple 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