How to set color for checkbox present in datagridview?

817 Views Asked by At

I have a datagridview in windows form. It looks like this.

enter image description here

In this, have one Checkbox column, two textbox column.

My requirement is need to set readonly true and set grey color for the checkbox for the country has Germany as like below.

enter image description here

Not like below

enter image description here

I need to set color for checkbox only not for datagridview cell.

Is anyone have idea for this?

2

There are 2 best solutions below

1
Rubio Jesús On

You can achieve that by setting checkbox's property: 'Enabled' as false. by that way, the checkbox cannot be clicked and its color changes automatically to grey. Example

0
Karen Payne On

Given the answer here and slightly change it

private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    var value = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
    DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[0];

    if (value == "Germany" && cell.ReadOnly != true)
    {
        DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
        chkCell.Value = false;
        chkCell.FlatStyle = FlatStyle.Flat;
        chkCell.Style.ForeColor = Color.DarkGray;
        cell.ReadOnly = true;
    }
}

Result

enter image description here