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="">
<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添加序号列,且支持分页连续累计显示的更多相关文章
- 使用模板技术处理ASP.NET中GridView额外序号列的问题
问题描述: 现在要在一张GridView表中添加一列来显示行号,要求是显示下一页的时候能够递增而不是从新编号.数据库中的没有相关序号列 如何在软件端实现呢? 通过测试,添加以下代码即可解决需求. &l ...
- bootstrap-table 增加序号列(支持分页)
columns: [ { checkbox: true }, { title: '序号', align: 'center', halign: 'center', formatter: function ...
- GridView,datalist添加序号列
GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...
- ASP.NET repeater添加序号列的方法
ASP.NET repeater添加序号列的方法 1.<itemtemplate> <tr><td> <%# Container.ItemIndex + 1% ...
- Repeater控件添加序号列
在项目开发过程中,会经常遇到ASP.NET repeater控件添加序号列,有些新手可能还不会,网上搜集整理了一些,需要的朋友可以参考下 ASP.NET repeater添加序号列的方法 1.< ...
- SQL - ROW_NUMBER,Rank 添加序号列
百度的时候查到的博客: http://blog.csdn.net/xsfqh/article/details/6663895-------------------------------------- ...
- element-UI el-table添加序号列时序号永远都是从1开始?
Part.1 示例 当我们想在 el-table 中添加序号列时,如下: <el-table-column label="序号" type="index" ...
- xls添加 序号列技巧
问题背景:在给xls添加一列序列时常碰到一个问题,用下拉很不科学(如果行数很多):用双击需要右边有一列不断开的数据列. 方法一:在A1 =row()回车,选择范围,快捷键 ctrl+d, 复制其文 ...
- jquery插件dataTables添加序号列
官网方法实例: $(document).ready(function() { var t = $('#example').DataTable({ "columnDef ...
随机推荐
- MySQL的慢查询分析
慢查询分析日最初是用来捕获比较“慢”的查询,在mysql5.1 + 版本中,慢查询的功能被加强,可以通过设置long_query_time为0来捕获所有的查询,而且查询的响应时间已经可以做到微妙级别. ...
- ld: library not found for -lPods-AFNetworking
工程新添加了 AFNetworking 使用pod ,pod install 完成后,编译报错 ld: library not found for -lPods-AFNetworkingclang: ...
- PHP查看SSL证书信息
<? $str = file_get_contents('2.cer'); print_r(openssl_x509_parse($str)); ?> 证书需要使用base64编码的方式c ...
- H-Basis/SG/SH GI Relighting
小试了一把预计算全局光照,作为PRT的上级应用.完全自行实现,使用SG/SH.H-Basis基波对GI光场进行频域压缩,存在3D纹理中,用于2跳间接光照实时显示.其中坑点不少,尤其是在HDR环境下使用 ...
- iOS中的webView加载HTML
在日常开发中,我们为了效率会用到很多很多的WebView,比如在做某个明细页面的时候我们返回给你的可能是一个html字符串,我们就需要将当前字符串展示到webView上面,所以我们对HTML标签需要有 ...
- .net core 1.0 实现负载多服务器单点登录
前言 .net core 出来有一时间了,这段时间也一直在做技术准备,目前想做一个单点登录(SSO)系统,在这之前用.net时我用习惯了machineKey ,也顺手在.net core 中尝试了一上 ...
- Ant自动编译打包&发布 android项目
Eclipse用起来虽然方便,但是编译打包android项目还是比较慢,尤其将应用打包发布到各个渠道时,用Eclipse手动打包各种渠道包就有点不切实际了,这时候我们用到Ant帮我们自动编译打包了. ...
- config中自定义配置
1. 定义自己的KeyValue <section name="TestKeyValue" type="System.Configuration.NameValue ...
- windows原生开发之界面疑云
windows桌面开发,界面始终是最大的困惑.我们对前端工具的要求,其实只有窗体设计器.消息映射,过分点的话自适应屏幕.模型绑定.能够免于手工书写,其实这个问题并不复杂,但VS不实现.QT语法 ...
- Winform 数据库连接app.config文件配置 数据库连接字符串
1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...