如何在c#.net的datagridview中设置整列的背景颜色(Readonly列的指示)

问题描述 投票:2回答:4

我有一个datagridview,我想在其中为两列设置readonlytrue。我想更改这些列的颜色。每当我离开细胞时,我只能使第一个细胞和当前细胞改变颜色。剩余的细胞不起作用。谁能帮我这个?

datagridview datagridviewcolumn
4个回答
3
投票

尝试

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

2
投票
    DataGridViewColumn dgv7col = dgv7.Columns[i];
    DataGridViewCell cell = new DataGridViewTextBoxCell();
    cell.Style.BackColor = Color.Wheat;
    dgv7col.CellTemplate = cell;

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


0
投票

简单:

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


0
投票
foreach (DataGridViewColumn col in dgv.Columns)
        if (col.ReadOnly) 
            col.DefaultCellStyle.BackColor = Color.Lavender;
© www.soinside.com 2019 - 2024. All rights reserved.