在gridview中實現隔行樣式轉換的方法
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//我們先設置當鼠標上去的時候他的背景色改變
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff6699'");
//下面我們再設置當鼠標離開后背景色再還原
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
/為特定的數改變行樣式這也是在這個事件里面,因為這個事件是在數據被綁定的時候執行的
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//為了對全部數據行都有用,我們使用循環 //
string lbl = Convert.ToString(DataBinder.eval_r(e.Row.DataItem,"state"));
//我們得取出行中state字段綁定的值,用他作為判斷條件 //
if (lbl == "BB") if (e.Row.RowIndex % 2 == 1)
{
//如果他的值等于BB,那么
e.Row.BackColor = Color.LimeGreen;
//給當前行的背景色賦值
}
}
}