为datagridview中的单元格上色

本文关键字:单元格 datagridview | 更新日期: 2023-09-27 17:50:50

我需要为DataGridView中的特定单元格上色。我试过了,但没有用。

dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Red;

但对于整个列它工作…

dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.Red;

但我需要一个单元格请如果你能帮助谢谢

为datagridview中的单元格上色

试试这个

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewCellStyle MakeItRed = new DataGridViewCellStyle();
MakeItRed.BackColor = Color.Red;
//make a whole column red
dataGridView1.Columns[1].DefaultCellStyle = MakeItRed;
//make a specific cell red
DataGridViewRow row2 = dataGridView1.Rows[2];
row2.Cells[2].Style = MakeItRed;
        }