在DataGrid_DataItemBound()事件中,
e.Attributes.Add("onmouseover","设置不同的背景色");

解决方案 »

  1.   

    首先我们知道datagrid在客户端被解释成了table所以我们有知道table都有tr和td组成,tr就是行,我们只需要在每个tr上面的onmouseover加入一段javascript脚本就可以实现这个功能,
    <table>
    <tr onmouseover="this.style.backgroundColor='Silver'" onmouseout="this.style.backgroundColor='white'"><td>...</td></tr>
    </table>
    这是从客户端看到的脚本那么我们可以通过datagrd在绑定数据的ItemDataBound事件时候将这段脚本加入进去。具体代码如下:
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
        e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='Silver'");
        e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='white'");
       }
    这样就可以让鼠标移动过行的时候将颜色变成silver移走之后变成white(本色)。
    前面的判断可以排除鼠标移动到Head和Foot的时候也有相同的效果,这样可以把脚本只产生在里面的项上。或者指定某列变色:
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
        e.Item.Cells[2].Attributes.Add("onmouseover","this.style.backgroundColor='Silver'");
        e.Item.Cells[2].Attributes.Add("onmouseout","this.style.backgroundColor='white'");
       }不但如此你还可以指定鼠标移动到某一列时鼠标的形状:
    e.Item.Cells[3].Style("cursor") = "hand"
    或者点击某一个单元个显示提示信息:
    e.Item.Cells[3].Attributes.Add("onclick", "alert('你点击的ID是: " + e.Item.Cells[0].Text + "!');")
    等等...通过这个方法我们还可以添加在鼠标移动到行上出现提示的效果
    e.Item.Cells[2].Attributes.Add("title","在这里可以添加提示信息");
      

  2.   

    在DataGrid_DataItemBound()事件中,
    e.Item.Attributes.Add("onmouseover","设置不同的背景色");