2

I have a datagridview in which I want to set readonly to true for two columns. I want to change the color for those columns. Whenever I leave from the cell I'm able to make only the first cell and current cell change color. Remaining cells are not working. Can any one help me with this?

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
user1858718
  • 137
  • 2
  • 3
  • 12

4 Answers4

3

try

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 0)
        if (dataGridView2[e.ColumnIndex, e.RowIndex].ReadOnly)
            e.CellStyle.BackColor = Color.Red;

    if (e.ColumnIndex == 1)
        if (dataGridView2[e.ColumnIndex, e.RowIndex].ReadOnly)
            e.CellStyle.BackColor = Color.Black;
}
spajce
  • 7,044
  • 5
  • 29
  • 44
2
    DataGridViewColumn dgv7col = dgv7.Columns[i];
    DataGridViewCell cell = new DataGridViewTextBoxCell();
    cell.Style.BackColor = Color.Wheat;
    dgv7col.CellTemplate = cell;

you have to define the column not the cell Ronny

rony sc
  • 71
  • 2
0

Simple:

if (grdView.Columns["Columnname"].ReadOnly) grdView.Columns["Columnname"].DefaultCellStyle.BackColor = Color.Lavender;

Juver Paredes
  • 124
  • 1
  • 2
0
foreach (DataGridViewColumn col in dgv.Columns)
        if (col.ReadOnly) 
            col.DefaultCellStyle.BackColor = Color.Lavender;