Search code examples
datagridviewdatagridviewcolumn

How to set the background color for an entire column(indication for Readonly column) in datagridview in c#.net


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?


Solution

  • 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;
    }