如何在 c#.net 的数据网格视图中设置整个列的背景颜色(只读列的指示)



我有一个数据网格视图,我想在其中设置readonly true两列。我想更改这些列的颜色。每当我离开单元格时,我只能使第一个单元格和当前单元格更改颜色。其余单元格不工作。任何人都可以帮我解决这个问题吗?

试试

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;

你必须定义列而不是单元格罗尼

简单:

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

foreach (DataGridViewColumn col in dgv.Columns)
        if (col.ReadOnly) 
            col.DefaultCellStyle.BackColor = Color.Lavender;

最新更新