private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int RowIndex = e.RowIndex;
// 确保是数据行,而不是表头行
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
// 如果当前是“名称”列
if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "名称")
{
// 检查单元格的值是否包含"Fail"
if (e.Value != null )
{
Color color = GlobalData.VelocityPlantIdents[RowIndex].Color;
// 设置该单元格的前景色
e.CellStyle.ForeColor = color;
// 你也可以同时设置背景色
// e.CellStyle.BackColor = Color.Yellow;
}
// 如果不满足条件,可以不设置,它会保持默认样式
}
}