首页 > 代码库 > ASP.NET 为GridView添加序号列,且支持分页连续累计显示
ASP.NET 为GridView添加序号列,且支持分页连续累计显示
为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Width="100%" PageSize="5"> <Columns> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <asp:Literal ID="Literal1" runat="server" Text=‘<%# (Container.DataItemIndex+1) %>‘></asp:Literal> </ItemTemplate> <ItemStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="#FF3300" Width="80px" /> </asp:TemplateField> <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="plateno" HeaderText="plateno" SortExpression="plateno" /> <asp:BoundField DataField="chassisno" HeaderText="chassisno" SortExpression="chassisno" /> <asp:BoundField DataField="brand" HeaderText="brand" SortExpression="brand" /> <asp:BoundField DataField="model" HeaderText="model" SortExpression="model" /> <asp:BoundField DataField="owner" HeaderText="owner" SortExpression="owner" /> <asp:BoundField DataField="telno" HeaderText="telno" SortExpression="telno" /> <asp:BoundField DataField="regdate" HeaderText="regdate" SortExpression="regdate" /> <asp:BoundField DataField="insurancecorp" HeaderText="insurancecorp" SortExpression="insurancecorp" /> <asp:BoundField DataField="insureddate" HeaderText="insureddate" SortExpression="insureddate" /> <asp:BoundField DataField="customertype" HeaderText="customertype" SortExpression="customertype" /> <asp:BoundField DataField="renewalby" HeaderText="renewalby" SortExpression="renewalby" /> <asp:BoundField DataField="csxcost" HeaderText="csxcost" SortExpression="csxcost" /> <asp:BoundField DataField="szxcost" HeaderText="szxcost" SortExpression="szxcost" /> <asp:BoundField DataField="sjxcost" HeaderText="sjxcost" SortExpression="sjxcost" /> <asp:BoundField DataField="ckxcost" HeaderText="ckxcost" SortExpression="ckxcost" /> <asp:BoundField DataField="dqxcost" HeaderText="dqxcost" SortExpression="dqxcost" /> <asp:BoundField DataField="blxcost" HeaderText="blxcost" SortExpression="blxcost" /> <asp:BoundField DataField="bjmpxcost" HeaderText="bjmpxcost" SortExpression="bjmpxcost" /> <asp:BoundField DataField="otherxcost" HeaderText="otherxcost" SortExpression="otherxcost" /> <asp:BoundField DataField="receivedsyxcost" HeaderText="receivedsyxcost" SortExpression="receivedsyxcost" /> <asp:BoundField DataField="receivedjqxcost" HeaderText="receivedjqxcost" SortExpression="receivedjqxcost" /> <asp:BoundField DataField="year" HeaderText="year" SortExpression="year" /> <asp:BoundField DataField="month" HeaderText="month" SortExpression="month" /> <asp:BoundField DataField="orgcode" HeaderText="orgcode" SortExpression="orgcode" /> <asp:BoundField DataField="attr1" HeaderText="attr1" SortExpression="attr1" /> <asp:BoundField DataField="attr2" HeaderText="attr2" SortExpression="attr2" /> <asp:BoundField DataField="attr3" HeaderText="attr3" SortExpression="attr3" /> <asp:BoundField DataField="importby" HeaderText="importby" SortExpression="importby" /> <asp:BoundField DataField="importbyid" HeaderText="importbyid" SortExpression="importbyid" /> <asp:BoundField DataField="importdatetime" HeaderText="importdatetime" SortExpression="importdatetime" /> </Columns> </asp:GridView> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ePFEDPConnectionString %>" SelectCommand="SELECT * FROM [T_RITargetCustomerInfo]"></asp:SqlDataSource> </form></body></html>
简要说明一下,由于我这里是作演示,所以我直接采用数据源SqlDataSource,大家仁者见仁,智者见智吧,实现自动生成序号的方法很多,最常见的是通过添加GridView1_RowDataBound方法,然后在里面依据实际情况计算序号,我这人希望能越简单且越好用就最好了,所以我采用了上面的方法,核心代码是:(Container.DataItemIndex+1),其中Container.DataItemIndex表示当前行索引,由于索引是从0开始,所以加上1就OK了,这样整个表就有序号了,而且在分页下也是连续性的,不会出现每页从1开始的情况。
效果如下:
另外需要说明的是,如果大家不是采用数据源控件,而是自己手动去绑定数据源的情况,那就不能简单按照方面的方法,原因是Container.DataItemIndex在手动绑定数据源时,会索引并不会记住,每次绑定均会重新从0开始,所以这时候我们需要按照当前的页码来进行计算,代码也很简单,如下:
<asp:TemplateField HeaderText="序号"> <ItemTemplate> <asp:Literal ID="Literal1" runat="server" Text=‘<%# ((GridView1.PageSize * GridView1.PageIndex) + Container.DataItemIndex +1) %>‘></asp:Literal> </ItemTemplate> <ItemStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="#FF3300" Width="80px" /> </asp:TemplateField>
更多IT相关的文章,欢迎光临我的个人网站:http://www.zuowenjun.cn/
ASP.NET 为GridView添加序号列,且支持分页连续累计显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。