在GV中常用到RowDataBound事件和写法.记下以备忘. if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标划过样式 e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#95b8e9';"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;"); DataRowView drv = (DataRowView)e.Row.DataItem; string sqlStr = "SELECT Flag from users where ID='" + drv.Row["ID"].ToString() + "'"; string Flag = DtDB.GetSingleValue(sqlStr); switch (Flag) { case "0": e.Row.BackColor = System.Drawing.Color.Red; break; case "1": e.Row.BackColor = System.Drawing.Color.Green; break; case "2": e.Row.BackColor = System.Drawing.Color.Gray; break; default: break; } //if (e.Row.Cells[2].Text.Trim().Length > 26) //{ //e.Row.Cells[7].ToolTip = e.Row.Cells[7].Text; //} } 附另一例 // 判断订单状态填充“操作”列 protected void gdvOrders_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView drv = (DataRowView)e.Row.DataItem ; if (drv.Row["Status"].ToString() == "审核中") { ((Label)e.Row.Cells[6].FindControl("lblOperator")).Text = "我要改单"; } if (drv.Row["Status"].ToString() == "已发货" && drv.Row ["ResponseDate"].ToString() == "") { ((Label)e.Row.Cells[6].FindControl("lblOperator")).Visible = false; ((LinkButton)e.Row.Cells[6].FindControl("lbtnAcceptGoods")).Visible = true; } } }