熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

鼠標移到GridView某一行時改變該行的背景色方法

2022-06-13   來源: .NET編程 

  效果圖


做法

雙擊GridView的OnRowDataBound事件
在後台的GridView_RowDataBound()方法添加代碼最後代碼如下所示
protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)
    {
        int i;
        //執行循環保證每條數據都可以更新
        for (i = ; i < GridViewRowsCount; i++)
        {
            //首先判斷是否是數據行
            if (eRowRowType == DataControlRowTypeDataRow)
            {
                //當鼠標停留時更改背景色
                eRowAttributesAdd(onmouseover c=thisstylebackgroundColor;thisstylebackgroundColor=#AFF);
                //當鼠標移開時還原背景色
                eRowAttributesAdd(onmouseout thisstylebackgroundColor=c);
            }
        }

  }

  前台代碼
<html xmlns= >
<head runat=server>
    <title>實現鼠標劃過改變GridView的行背景色 清清月兒 </title>
</head>
<body>
    <form id=form runat=server>
    <div>
        <asp:GridView ID=GridView runat=server AutoGenerateColumns=False DataKeyNames=身份證號碼
            DataSourceID=SqlDataSource AllowSorting=True BackColor=White BorderColor=#CCCCCC BorderStyle=None BorderWidth=px CellPadding= FontSize=px OnRowDataBound=GridView_RowDataBound>
            <Columns>
                <asp:BoundField DataField=身份證號碼 HeaderText=身份證號碼 ReadOnly=True SortExpression=身份證號碼 />
                <asp:BoundField DataField=姓名 HeaderText=姓名 SortExpression=姓名 />
                <asp:BoundField DataField=家庭住址 HeaderText=家庭住址 SortExpression=家庭住址 />
                <asp:BoundField DataField=郵政編碼 HeaderText=郵政編碼 SortExpression=郵政編碼 />
            </Columns>
            <FooterStyle BackColor=White ForeColor=# />
            <RowStyle ForeColor=# />
            <SelectedRowStyle BackColor=# FontBold=True ForeColor=White />
            <PagerStyle BackColor=White ForeColor=# HorizontalAlign=Left />
            <HeaderStyle BackColor=# FontBold=True ForeColor=White />
        </asp:GridView>
        <asp:SqlDataSource ID=SqlDataSource runat=server ConnectionString=<%$ ConnectionStrings:北風貿易ConnectionString %>
            SelectCommand=SELECT top [身份證號碼] [姓名] [員工性別] [家庭住址] [郵政編碼] FROM [飛狐工作室] DataSourceMode=DataReader></asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


From:http://tw.wingwit.com/Article/program/net/201311/12215.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.