'How to set color for checkbox present in datagridview?
I have a datagridview in windows form. It looks like this.
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.
Not like below
I need to set color for checkbox only not for datagridview cell.
Is anyone have idea for this?
Solution 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rubio Jesús |
Solution 2 | Karen Payne |