日期:2014-05-18  浏览次数:20450 次

gridview模板列颜色设置
在我的GridView里有一列模板列,是放的文本框HtmlInputText
当数据绑定时,如果某文本框的值小于0,让这个文本框的字体颜色自动的显示为红色
这改这么弄啊,搞的我头都大了,各位大哥帮帮忙啊

------解决方案--------------------
在databind事件里写模板的style
------解决方案--------------------
if (tableForumSubject!=null)
{
for (int i = 0; i < tableForumSubject.Rows.Count; i++)
{
if (tableForumSubject.Rows[i]["IsDistillate"].ToString().Equals("001"))
{
tableForumSubject.Rows[i]["IsDistillate"] = "<font color='blue'>是</font>";
}
else
{
tableForumSubject.Rows[i]["IsDistillate"] = "<font color='green'>否</font>";
}
if (tableForumSubject.Rows[i]["IsLock"].ToString().Equals("001"))
{
tableForumSubject.Rows[i]["IsLock"] = "<font color='red'>锁定</font>";
}
else
{
tableForumSubject.Rows[i]["IsLock"] = "<font color='green'>未锁</font>";
}
if (tableForumSubject.Rows[i]["IsShield"].ToString().Equals("001"))
{
tableForumSubject.Rows[i]["IsShield"] = "<font color='red'>已屏蔽</font>";
}
else
{
tableForumSubject.Rows[i]["IsShield"] = "<font color='green'>未屏蔽</font>";
}
}
this.wecSubjectPage.PageName = "DefaultModel/Forum/ForumAdminSpaceSubjectList.aspx?ForumSpaceId=" + forumSpaceId + "&";
this.grvForumSpieceSubject.DataSource = this.wecSubjectPage.BindGrid(tableForumSubject.DefaultView);
this.grvForumSpieceSubject.DataBind();
}
在html模版列添加HtmlEncode="False"
------解决方案--------------------
你的那个错误是因为不存在Cells[10]这个单元格。检查一下一行有几个单元格,注意索引是从0开始的。
------解决方案--------------------
textbox txt=e.row.cell[i].findcontrol("txtname")
if(txt.text<10)
{
txt.text="red";
}
------解决方案--------------------
那就在绑定事件里 直接写

e.row.cell[i].text="<input>"
------解决方案--------------------
C# code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HtmlInputText txt = (HtmlInputText)e.Row.FindControl("Text1");
            if (Convert.ToInt32(txt.Value) < 0)
            {
                txt.Style.Add("color", "red");
            }
            
        }
    }

------解决方案--------------------
13楼回答正确,要用到Rowdatabind事件
LZ要理解rowdatabind事件在什么时间触发,以及主要用途.
Rowdatabind是加载GRIDVIEW控件时引发的