添加CSS链接:
   <link href="./DataGrid/Grid.css" type="text/css" rel="stylesheet" />
页面代码如下:

 <asp:DataGrid ID="MyDataGrid" runat="server"  AutoGenerateColumns="False" CssClass="Grid" 
                                HeaderStyle
-CssClass="GridHeader" Width="100%" OnItemDataBound="MyDataGrid_ItemDataBound"  >
                                
<SelectedItemStyle   />
                                
<ItemStyle CssClass="ItemStyle" />
                                
<HeaderStyle CssClass="GridHeader"  Height="20px" HorizontalAlign="Center" VerticalAlign="Middle"   />
                                
<AlternatingItemStyle   BorderColor=""  BackColor="#fcfcfc"></AlternatingItemStyle>
                                
<Columns >
                                
<asp:BoundColumn DataField=""  ItemStyle-Width="25" HeaderText="" >
                                 
<ItemStyle CssClass="FirstCell" Height="20px" />
                                    
</asp:BoundColumn>
                                    
<asp:BoundColumn DataField="UserID"  HeaderText="设备编号" >
                                        
<ItemStyle CssClass="ItemStyle" Height="20px" />
                                    
</asp:BoundColumn>
                                    
<asp:BoundColumn DataField="UserID"  HeaderText="设备编号" >
                                        
<ItemStyle CssClass="ItemStyle" Height="20px" />
                                    
</asp:BoundColumn>
                                      
<asp:BoundColumn DataField="UserID"  HeaderText="设备编号" >
                                        
<ItemStyle CssClass="ItemStyle" Height="20px" />
                                    
</asp:BoundColumn>
                                  
                                   
                                
</Columns>
         
<EditItemStyle CssClass="SelectedRow td.DataCell " />
         
<AlternatingItemStyle CssClass="AlternatingRow" />
                            
</asp:DataGrid>

后台代码:
protected void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    
{
        
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        
{
            e.Item.Attributes.Add(
"onmouseover""this.style.backgroundColor='#f0f0f0';this.style.color='buttontext';this.style.cursor='hand'");
          
        }

        
if (e.Item.ItemType == ListItemType.Item)
        
{
            e.Item.Attributes.Add(
"onmouseout""this.style.backgroundColor=''");
        }

        
else
        
{
            e.Item.Attributes.Add(
"onmouseout""this.style.backgroundColor='#fcfcfc'");
        }

        
       
//this.MyDataGrid.AlternatingItemStyle.BackColor=System.Drawing.Color.White;
       
// this.MyDataGrid.BackColor=System.Drawing.Color.Gainsboro;
       
    }
样式表:
.Grid 

  background
-image: url(images/grid_bg.gif); 
  background
-color: #FFFFFF; 
  border: 1px solid #C0C0C0; 
  border
-top-width: 0px;
  border
-left-width: 0px;
  cursor: pointer; 
  
}


.GridHeader
{
  background
-image: url(images/header_rowBg.gif); 
  background
-color: #8988A5; 
  border: 1px solid #57566F; 
  height: 28px;
  padding
-left: 3px;
  cursor: 
default
  text
-align:center;
  font
-size:14px;
  font
-style:inherit;
  font
-weight:bolder;
}

.ItemStyle 

    border
-bottom-style:solid;
    border
-left-style:none;
    border
-right-style:solid;
    border
-top-style:none;
    border
-color:#E6E6EE;
    text
-align:center;
    border
-width:1px;

}

.FirstCell

  background
-color: #D6D7E1; 
  background
-image: url(images/headingSelectorCell_bg.gif); 
  border
-width: 0px; 
  border
-right: 1px solid #FFFFFF; 
}

.AlternatingRow
{
    
  background
-color: #fcfcfc; 
  border
-color: white; 
}

 

代码下载

预览如下:

posted on 2007-03-29 09:36  过江  阅读(1756)  评论(1编辑  收藏  举报