日期:2014-05-19  浏览次数:20534 次

写了一个datagrid,当鼠标放上去那行值的背景颜色就变,但是变会来的不对
我在这个论坛上,看见有人发了鼠标方上面datagrid颜色就变的代码,但是虽然变了,但是变回来的不对...
我做了一个当鼠标放在上面,那行的datagrid就变色,但是他变色后,就便不会来了,因为我的datagrid是用了一个模版。是交叉颜色变换的那种。  
当鼠标移开,他只会变成datagrid中其中的一种颜色,导致以前的datagrid是交换的颜色,现在变成只是一种颜色的样式。不知道怎么改  

代码:  
cs:  
private   void   DataGrid1_ItemDataBound(object   sender,   System.Web.UI.WebControls.DataGridItemEventArgs   e)  
{  
e.Item.Attributes.Add(   "onmouseout ", "this.style.backgroundColor=\ " "+e.Item.Style[ "BACKGROUND-COLOR "]+ "\ " "   );  
e.Item.Attributes.Add(   "onmouseover ", "this.style.backgroundColor=\ " "+   "#B9D9FB "+ "\ " "   );  
}  

aspx:  

<asp:datagrid   id= "DataGrid1 "   style= "Z-INDEX:   102;   LEFT:   120px;   POSITION:   absolute;   TOP:   160px "  
Font-Size= "Larger "   HorizontalAlign= "Center "   GridLines= "Vertical "   BorderColor= "#999999 "   BorderStyle= "None "  
BorderWidth= "1px "   BackColor= "White "   CellPadding= "3 "   Width= "150px "   Height= "500px "   AutoGenerateColumns= "False "  
runat= "server ">  
<SelectedItemStyle   Font-Bold= "True "   ForeColor= "White "   BackColor= "#008A8C "> </SelectedItemStyle>  
<AlternatingItemStyle   BackColor= "Gainsboro "> </AlternatingItemStyle>  
<ItemStyle   ForeColor= "Black "   BackColor= "#EEEEEE "> </ItemStyle>  
<HeaderStyle   Font-Bold= "True "   HorizontalAlign= "Center "   ForeColor= "White "   VerticalAlign= "Middle "  
BackColor= "#000084 "> </HeaderStyle>  
<FooterStyle   ForeColor= "Black "   BackColor= "#CCCCCC "> </FooterStyle>  
<Columns>  
<asp:TemplateColumn   HeaderText= "A-E ">  
<ItemTemplate>  
<asp:Image   Runat= "server "   ImageUrl= "\yinyue\img\bul_05.gif "> </asp:Image>  
<asp:HyperLink   runat= "server "   ToolTip= ' <%#   DataBinder.Eval(Container,   "DataItem.nname ")   %> '   Text= ' <%#   DataBinder.Eval(Container,   "DataItem.nname ")   %> '   NavigateUrl= ' <%#   DataBinder.Eval(Container,   "DataItem.nname ",   "zhanshi.aspx?name={0} ")   %> '>  
</asp:HyperLink>  
</ItemTemplate>  
</asp:TemplateColumn>  
</Columns>  
<PagerStyle   HorizontalAlign= "Center "   ForeColor= "Black "   BackColor= "#999999 "   Mode= "NumericPages "> </PagerStyle>

应该是不难吧~~~~~~
感觉关键的代码在这
e.Item.Attributes.Add(   "onmouseout ", "this.style.backgroundColor=\ " "+e.Item.Style[ "BACKGROUND-COLOR "]+ "\ " "   );  
这个变会来的只是datagrid中的一种交叉颜色~~

------解决方案--------------------
请参考

private void DataGridView_ItemDataBound(object send