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

StackOverflow https://stackoverflow.com/questions/13908200

  •  09-12-2021
  •  | 
  •  

Question

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?

Était-ce utile?

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

Autres conseils

    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

Simple:

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

foreach (DataGridViewColumn col in dgv.Columns)
        if (col.ReadOnly) 
            col.DefaultCellStyle.BackColor = Color.Lavender;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top