我可以改变回来的GridView行已programmaticly绑定的颜色? [英] Can I change back color of gridview row which is programmaticly databinding?

查看:123
本文介绍了我可以改变回来的GridView行已programmaticly绑定的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个gridview

I have a gridview like this

        <asp:GridView ID="GridView1" runat="server" 
            DataSourceID="SqlDataSource1">
        </asp:GridView>

和我结合它的Page_Load

and I bind it on page_load

protected void Page_Load(object sender, EventArgs e)
    {
       SqlDataSource1.SelectCommand = "select * from table"
    }

和桌子我有一个字段日期

and at the table I have a field 'date'

+-----+
|date |
+-----+
|date1|
+-----+
|date2|
+-----+

和我想要做的控制--->如果DATE1&LT;现在gridviewrows背景色=红

and I want do that control---> if date1 < now gridviewrows backcolor = red

我就是这么做的。

protected void GridViewServicesList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      DateTime date = 
          Convert.ToDateTime(e.Row.Cells[indexOfDateField].Text);//returns null!!!
      if(date < DateTime.Now)
        {
          e.Row.BackColor = Color.Red;
        }

    }

和这是行不通的,我应该怎么办?
首先,我甚至不能访问日期。我的意思是可变的DateTime日期为空...
顺便一提
这不是我的真正的鳕鱼,我把它写了basicly理解。

and this is not working,What should I do? Firstly I cant even access date. I mean the variable DateTime date is null... By the way this is not my real cod, I write it for basicly understanding.

推荐答案

如果该DATE1标签在一个TemplateField,使用下面的示例。

If the date1 label is in a TemplateField, the use the sample below.

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        //Reference the date1 label in Gridviews template field
        System.Web.UI.WebControls.Label date1 = (System.Web.UI.WebControls.Label)e.Row.FindControl("date1");
        GridViewRow myRow = e.Row;
        //set back color to green if incident category is hazard
        if (date1 < DateTime.Now)
             {
               e.Row.BackColor = Drawing.Color.SpringGreen;
            }   
    }
}

这篇关于我可以改变回来的GridView行已programmaticly绑定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆