我想将datagrid中绑定的数据库记录按时间条件显示相应的颜色,怎么做呢?

解决方案 »

  1.   

    每条记录里都包含有一个时间字段,根据此时间字段判断在WEB页面中显示本条记录的颜色。
    例如今天的显示红色,昨天的显示黑色。
      

  2.   

    参考一下:
    private void grd_user_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
      if(e.Item.Cells[8].Text!="6")
    {
    e.Item.Attributes.Add("onMouseOver","this.style.backgroundColor ='#f0f0f0'"); }
    }
      

  3.   

    在DataGrid的ItemDataBound事件中添加代码,你可以根据程序先检索出需要改变颜色的行,在添加代码,可以参考一下代码:
    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
    {
    e.Item.Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF'");
    e.Item.Attributes.Add("onmousedown", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF'");
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
    for (int i = 0; i< grdJieGuo.Columns.Count; i++ )
    {
    e.Item.Cells[i].Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#99ccff'");
    //e.Item.Cells[i].Attributes.Add("onmousedown", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#99ccff'");
    e.Item.Cells[i].Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
    }
    }
      

  4.   

    如果用REPEATER的话,可以:
    <asp:repeater id=list runat=server>
    <itemtemplate>
    <font color='<%# getcolor((DataBinder.Eval(Container.DataItem,"date_time")).ToString())%>'>
    <%# DataBinder.Eval(Container.DataItem,"字段")%>
    </font>
    </itemtemplate>
    </asp:repeater>
    后台:
     public string getcolor(string d_time)
    {
      if (Convert.ToDateTime(d_time)>DateTime.now)
    {
     retrun "red";
    }
    else 
    {
      return "blue";
     }
    }
      

  5.   

    ItemDataBound事件对时间所在列进行判断,分别赋于当前行的背景色