鼠标移动DataGrid控件某行改变该行的背景

在DataGrid控件的数据项的Attributes属性为数据项添加Javascript脚本即可
 (1)   触发onmouseover事件时,执行如下代码设置当前DataGrid项的背景色
               thiscolor = this.style.backgroundColor,this.style.backgroundColor='#ddf3ff'

 (2)   当触发onmouseout事件时,执行如下代码还原前面的背景色
            this.style.backgroundColor = thiscolor

在DataGrid控件的ItemDataBound事件

 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
     //如果当前项不为空
   if(e.Item.ItemIndex != -1)
   {
    //取得当前索引值加1,因为索引是从0开始,给DataGrid自动编号
    int orderID = e.Item.ItemIndex +1;
    e.Item.Cells[1].Text = orderID.ToString();

   }
   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    e.Item.Attributes.Add("onmouseover","thiscolor = this.style.backgroundColor,this.style.backgroundColor='#ddf3ff'
");
    e.Item.Attributes.Add("onmouseout"," this.style.backgroundColor = thiscolor
");

  }
  }

原文地址:https://www.cnblogs.com/conquer/p/554388.html