GridView,Repeater增加自动序号列

有三种实现的方式,

第一种方式,直接在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" />
                           <asp:CommandField HeaderText="删除"ShowDeleteButton="True" />
                       </Columns>
                       <RowStyle ForeColor="#000066"/>
                       <SelectedRowStyle BackColor="#669999"Font-Bold="True" ForeColor="White" />
                       <PagerStyle BackColor="White" ForeColor="#000066"HorizontalAlign="Left" />
                       <HeaderStyle BackColor="#006699" Font-Bold="True"ForeColor="White" />
                   </asp:GridView>

这里基本上介绍的已经够用了,详细试试;

Repeater自身就带有这个获取当前行号的属性,而无需程序员绑定这个行号,Container.ItemIndex就可以获取了,见下示例:

<asp:Repeater ID="Repeater1"runat="server">
   <ItemTemplate>
       行号:<%#Container.ItemIndex %>
   </ItemTemplate>
</asp:Repeater>

如果上面的示例中,Repeater已经绑定了数据,并且数据的至少为一笔记录,那么行号就会显示出来,行号从零开始,如果想改为从1开始,那么可以将以上的代码改为Container.ItemIndex+1,见如下示例:

<asp:Repeater ID="Repeater1"runat="server">
   <ItemTemplate>
       行号:<%#Container.ItemIndex+ 1%>
   </ItemTemplate>
</asp:Repeater>

就可以了。

<asp:RepeaterID="Repeater1"runat="server">
   
<ItemTemplate>
       
<%#Container.ItemIndex+ 1%>
       
<%#(Containeras RepeaterItem).ItemIndex+1%>
   
</ItemTemplate>
</asp:Repeater>

赠送一个小技巧:

Repeater数据为空时提示暂无数据

<FooterTemplate>  
       
<asp:Label ID="lblEmpty" Text="暂无数据"runat="server" Visible='<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>'></asp:Label> 
                
 </FooterTemplate>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值