C# – How to disable particular check box cell in a DataGridView CheckBox column

cdatagridviewwinforms

I have a winForm with a DataGridView control. It contains 5 columns, one of them is a CheckBox column. I want to enable/disable checkbox cell of this column based on the value present in another column at the same row.

I can disable entire column using DisabledCheckBoxCell

But it makes entire column in disabled state.

Here is a snippet of DataGridView,

SourceColumn | DestinationColumn
true                  | enabled

true                  | enabled
false                 | disabled

Does anyone have idea, how this can be achieved in .Net.

Best Answer

Vijay,

DataGridViewCheckBoxColumn does not have property called disabled so by changing the style of the checkbox you can make it look like as if it is disabled. Look at the following code.

 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        if (e.RowIndex == 1)
        {
            DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
            DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
            chkCell.Value = false;
            chkCell.FlatStyle = FlatStyle.Flat;
            chkCell.Style.ForeColor = Color.DarkGray;
            cell.ReadOnly = true;

        }

    }
Related Topic