经测试,代码无问题。
 int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString());
这句你判断一下为null 的情况跳过循环。        public Form1()
        {
            InitializeComponent();            dataGridView1.Rows.Add("1", "30", "30", "30");
            dataGridView1.Rows.Add("1", "40", "70", "40");
            dataGridView1.Rows.Add("1", "70", "60", "30");
        }        public void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
                int s = Convert.ToInt16(dataGridView1.Rows[i].Cells[1].Value.ToString());                if (s > 60)
                {                    this.dataGridView1.Rows[i].Cells[1].Style.BackColor = Color.Red;
                }
            }
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[1].Value == null) continue;
                int m = Convert.ToInt16(dataGridView1.Rows[i].Cells[2].Value.ToString());                if (m > 60)
                {                    this.dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;                }            }
        }

解决方案 »

  1.   

    绑定数据之后直接执行你的循环,不要写到CellFormatting事件里
      

  2.   

    CellFormatting是在单元格格式化字符串的时候触发执行
    而如果你表格里根本没有设置格式化字符串,这个事件应该根本没执行
      

  3.   

    应该是!=null 时,continue吧,但也不行
      

  4.   


         不是这的问题,但是我搞错了continue的定义是==null,跳过此次循环,继续执行下次循环的意思,见笑了。
      

  5.   

    如果是null,就直接报错了才对吧
    既然不报错,你断点调试,看代码确实执行了吗?
    没执行就别纠结为什么不显示了
      

  6.   

    不是null的问题,正如你所说的,我删掉了CellFormatting,而且只用一个if循环,就实现了。太感谢了!