我有一个datagridview,我想在其中为两列设置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;
你必须定义列而不是单元格Ronny
简单:
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;