我想测试 DataGridView 中的行颜色变化,所以我编写了该代码:
dataGridView1.Rows.Add(new object[] { "Uno", "No" });
dataGridView1.Rows.Add(new object[] { "Due", "No" });
dataGridView1.Rows.Add(new object[] { "Tre", "Yes" });
dataGridView1.Rows.Add(new object[] { "Quattro", "No" });
dataGridView1.Rows.Add(new object[] { "Cinque", "Yes" });
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[1].Value.ToString() == "Yes")
row.DefaultCellStyle.ForeColor = Color.Red;
else
row.DefaultCellStyle.ForeColor = Color.Green;
}
}
所以,有五行两列。但是当我试图改变颜色时,它给了我一个 NullReference 异常,说 row.Cells[1] 值为空。怎么了?