您的位置:首页 > 编程语言 > ASP

GridView,Repeater增加自动序号列

2013-12-24 10:54 295 查看
有三种实现的方式,

第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.

<Columns>         

<asp:TemplateField HeaderText="序号"InsertVisible="False">

             <ItemStyle HorizontalAlign="Center"/>

             <HeaderStyle HorizontalAlign="Center" Width="5%"/>

            <ItemTemplate>

             <%#Container.DataItemIndex+1%>

           </ItemTemplate>

</asp:TemplateField>

</Columns>

第二种方式分页时进行了计算,这样会累计向下加.

           <asp:TemplateField HeaderText="序号"InsertVisible="False">

             <ItemStyle HorizontalAlign="Center"/>

             <HeaderStyle HorizontalAlign="Center" Width="5%"/>

            <ItemTemplate>

                <asp:Label ID="Label2" runat="server"Text='<%#this.MyListGridView.PageIndex * this.MyListGridView.PageSize +this.MyListGridView.Rows.Count
+1%>'/>

           </ItemTemplate>

           </asp:TemplateField>

第三种方式

还有一种方式放在cs代码中,和第二种相似.

   <asp:BoundField HeaderText="序号">

             <ItemStyle HorizontalAlign="Center"/>

             <HeaderStyle HorizontalAlign="Center" Width="5%"/>

         </asp:BoundField>

       protected void myGridView_RowDataBound(object sender,GridViewRowEventArgs e)

       {

           if (e.Row.RowIndex != -1)

           {

               int indexID =this.myGridView.PageIndex * this.myGridView.PageSize +e.Row.RowIndex + 1;

                e.Row.Cells[0].Text =indexID.ToString();
            }

        }

第四种方法

  双击GridView的OnRowDataBound事件;

在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:

    protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgse)

    {

       //如果是绑定数据行 //清清月儿http://blog.csdn.net/21aspnet 

       if (e.Row.RowType == DataControlRowType.DataRow)

       {

           ////鼠标经过时,行背景色变

           //e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#E6F5FA'");

           ////鼠标移出时,行背景色变

           //e.Row.Attributes.Add("onmouseout","this.style.backgroundColor='#FFFFFF'");

           ////当有编辑列时,避免出错,要加的RowState判断

           //if (e.Row.RowState == DataControlRowState.Normal ||e.Row.RowState == DataControlRowState.Alternate)

           //{

           //   ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick","javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text +"\"吗?')");

           //}
       }

       if (e.Row.RowIndex != -1)

       {

           int id = e.Row.RowIndex + 1;

           e.Row.Cells[0].Text = id.ToString();

       }
    }
注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。

<asp:GridView ID="GridView1" runat="server" CellPadding="3"OnRowDeleting="GridView1_RowDeleting"OnRowEditing="GridView1_RowEditing"

                       OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White"BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px"Font-Size="12px"OnRowDataBound="GridView1_RowDataBound">

                       <FooterStyle BackColor="White" ForeColor="#000066"/>

                       <Columns>

                           <asp:BoundField DataField="身份证号码" HeaderText="编号"ReadOnly="True" />

                           <asp:BoundField DataField="姓名" HeaderText="用户姓名"/>

                           <asp:BoundField DataField="员工性别" HeaderText="性别"/>

                           <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"/>

                           <asp:CommandField HeaderText="选择"ShowSelectButton="True" />

                           <asp:CommandField HeaderText="编辑"ShowEditButton="True" />

                   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息