日期:2014-05-17  浏览次数:20373 次

gridview后台控制某列的样式
如题,想在后台控制某列的样式,比如当那列含有某个关键字时,显示为红色,是在 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

}
中写的吧,能给点提示吗?先谢谢了

------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
if (e.Row.Cells[0].Text.Contains("关键字")) e.Row.Attributes.Add("style", "color:red");
}
}
------解决方案--------------------
你这种写法不好控制,你最好设置个控件



<asp:TemplateField><ItemTemplate><tr style='HEIGHT: 10px;<asp:Literal id=xx runat=server/>'>

if (e.Row.RowType == DataControlRowType.DataRow) {
if (e.Row.Cells[0].Text.Contains("关键字"))
{
 Literal xx = e.Row.FindControl("xx") as Literal;
xx.Text = "color:red";
}
}
------解决方案--------------------
楼主可以用datalist,不用再添加控件!我以前做过关键字查询的,关键字返回红色,你看看下面的例子:
C# code

DataTable dt =返回的表
for(int i = 0 ;i<dt.Rows.count;i++){循环表各行

 dt.Rows[i]["列名"] = (dt.Rows[i]["列名"] + "").Replace(Tkey.Text.ToString(), "<FONT color=#ff0000>" + Tkey.Text.ToString() + "</FONT>");
}
datalist1.DataSource=dt;
datalist1.DataBind();