环境:VS2008,C#
问题:
      从数据库中提取表信息填充到单元格内,数据表里有条时间记录(格式:YYY-MM-DD)读取到GridView后,我想通过判断当前时间改变日期字体颜色 或者单元格背景色,比如,表中时间>当前时间,显示为蓝色字体,否则显示为红色字体,或者修改单元格背景色为红色或者蓝色,请兄弟姐妹告诉怎么实现

解决方案 »

  1.   

    加入IF判断语句,在前台加入,像<% if(1>2)%>  <%{%>  1不大于2 <%}%>
      

  2.   

    if (e.Row.RowType == DataControlRowType.DataRow)
                {
    if(时间>系统时间){
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF';");}
    else
    {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor =currentcolor");
                }
    }
      

  3.   

    判断时间呗  时间判断过以后想改变颜色的话加个<span>标签就可以了
      

  4.   

    不好意思,上面是鼠标悬浮的颜色,适当改下就可以,
    e.Row.Attributes.Add("this.style.backgroundColor", "#00A9FF");}
      

  5.   

    在RowDataBind中判断。<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            </asp:GridView>public class Demo
            {
                public string ID { set; get; }
                public DateTime Time { set; get; }
            }        protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    this.GridView1.DataSource = new System.Collections.Generic.List<Demo>()
                    { 
                          new Demo(){ ID="S001", Time=DateTime.Now.AddDays(-1)},
                          new Demo(){ ID="S002", Time=DateTime.Now.AddDays(1)},
                          new Demo(){ID="S003", Time=DateTime.Now.AddDays(-2)},
                          new Demo(){ID="S004", Time=DateTime.Now.AddDays(2)},
                          new Demo(){ID="S005", Time=DateTime.Now.AddHours(2)},
                          new Demo(){ID="S006", Time=DateTime.Now.AddHours(1)},
                    };
                    this.GridView1.DataBind();
                }
            }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
                    {
                        DateTime time = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Time"));
                        if (time > DateTime.Now)
                        {
                            e.Row.Cells[1].Attributes.Add("style", "background:blue;");
                        }
                        else
                        {
                            e.Row.Cells[1].Attributes.Add("style", "background:red;");
                        }
                    }
                }
            }