设置GridView行的背景色以及鼠标经过时背景色变换
做个根据时间变化比较而用颜色表示的GridView,主要是通过RowDataBound事件来操作:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//判断是否是DataRow,以防止鼠标经过Header是也有效果
{
int tempdate = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "DayCount"));//获取时间比较的结果值
if (tempdate > 20 && tempdate < 841)
{
//设置背景色
e.Row.BackColor = Color.Red
//鼠标经过时,背景色变换
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#CDCD00';this.style.color='#884';this.style.cursor='#884'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Red';this.style.color='black';this.style.cursor='blue'");
}
else if (tempdate > 841)
{
e.Row.BackColor = Color.Turquoise;
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#CDCD00';this.style.color='#884';this.style.cursor='#884'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Turquoise';this.style.color='black';this.style.cursor='blue'");
}
else
{
e.Row.BackColor = Color.White;
}
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)//判断是否是DataRow,以防止鼠标经过Header是也有效果
{
int tempdate = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "DayCount"));//获取时间比较的结果值
if (tempdate > 20 && tempdate < 841)
{
//设置背景色
e.Row.BackColor = Color.Red
//鼠标经过时,背景色变换
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#CDCD00';this.style.color='#884';this.style.cursor='#884'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Red';this.style.color='black';this.style.cursor='blue'");
}
else if (tempdate > 841)
{
e.Row.BackColor = Color.Turquoise;
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#CDCD00';this.style.color='#884';this.style.cursor='#884'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Turquoise';this.style.color='black';this.style.cursor='blue'");
}
else
{
e.Row.BackColor = Color.White;
}
}
}