我们可以在Gridview的RowDataBound事件中来处理相关颜色的显示! 很简单,看代码就能明白!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"));
//满足特定要求的数据显示不同的颜色
if (str=="duoduo")
{
e.Row.BackColor = System.Drawing.Color.Beige;
}
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
//鼠标经过或者离开时候显示不同的颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='red',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"));
//满足特定要求的数据显示不同的颜色
if (str=="duoduo")
{
e.Row.BackColor = System.Drawing.Color.Beige;
}
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
//鼠标经过或者离开时候显示不同的颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='red',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
}