private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "shijian")
            {
                DateTime hjshijian = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                if (hjshijian<= DateTime.Now)
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                }
            }
        }运行后全部变红了,如果改为if (hjshijian.TimeOfDay >= DateTime.Now.TimeOfDay)显示反的正确,什么问题?该怎样做?

解决方案 »

  1.   

    if (hjshijian<= DateTime.Now)比较的日期和时间,而if (hjshijian.TimeOfDay >= DateTime.Now.TimeOfDay)只比较时间,所以就正确了。你到底要比较日期还是时间?
      

  2.   

    不好意思,if (hjshijian>= DateTime.Now)也是正确的显示,就是跟我的要求相反,我前面将.timeofday删了后面的忘了删。
      

  3.   

    if (时间1.CompareTo(时间2) < 0)
    {
       时间1 < 时间2;
    }
    用这看看,应该可以
      

  4.   

    改成这样
                    if (hjshijian.CompareTo(xianzaishijian)>0)
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                    }
    还是大于号正确小于号错误,有鬼了
      

  5.   

    按理说不会。compare(old,new) or old.comareto(new)0 equal
    1 old greater then new
    -1 new greater then old
      

  6.   

    DateTime.Now 显示的是当前的日前,你如果按天比较的话应该是
    if(hjshijian<DataTime.Now.AddDay(1))
      

  7.   

      DateTime hjshijian = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells["shijian"].Value);  if (hjshijian<= DateTime.Now)
      {
      dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
      }
      }
      

  8.   

     加了else后正常了
     if (hjshijian<= DateTime.Now)
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
                    }
                    else
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
                    }
    可是为什么呢?