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

문제

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?

도움이 되었습니까?

해결책

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

다른 팁

    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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top