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/

时间: 2024-08-28 10:24:55

ASP.NET 为GridView添加序号列,且支持分页连续累计显示的相关文章

GridView添加序号列

在GridView控件第0列添加序号列: <asp:BoundField HeaderText="序号" > <ItemStyle HorizontalAlign="Center" /> <HeaderStyle HorizontalAlign="Center" Width="30px" /> </asp:BoundField> 为GridView添加OnRowDataBound

给DataTable添加序号列

如何在datatable中添加一序号列,编号从1依次递增,并且在第一列? /// <summary>        /// 在DataTable中添加一序号列,编号从1依次递增        /// </summary>        /// <param >DataTable</param>        /// <returns></returns>        private DataTable AddSeriNumToDat

AspxGridView添加序号列

using System.Globalization;//CultureInfo需要 protected void AspxGridView_stock_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.Caption.Trim() == "序号") { e.Cell.Text = ((e.Vis

ASP.NET repeater添加序号列的方法

1.<itemtemplate> <tr><td> <%# Container.ItemIndex + 1%> </td></tr> </itemtemplate> 2.<itemtemplate> <tr><td> <%# this.rpResult.Items.Count + 1%> </td></tr> </itemtemplate>

ASP.NET 关于GridView 表格重复列合并

这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下GridView 和 Repeater 关于重复数据合并的方法. 效果图如下 : GridView : 前台代码 : 1 <div> 2 <asp:GridView ID="gvIncome" runat="server" AutoGenerateColumns="False"> 3 <Columns> 4 <asp:Te

RadGridView添加序号列

public class RowNumberColumn : GridViewDataColumn { public override System.Windows.FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem) { TextBlock textBlock = cell.Content as TextBlock; if (textBlo

dataTable添加序号列

使用render( data, type, row, meta )的参数meta 直接新加一个列 columns: [ {data: "id", title: "id", visible: false, searchable: false}, {data: null, tile: "序号", visible: true, searchable: false}, {data: "name", title: "名称&qu

oracle 自动添加序号列 排序

select      HSL.sortno,                    HSL.B,                    HSL.A,                    row_number() over(order by sortno desc) xh               from (select  t.B, t.A ,                            case                              when t.B = '

给mysql查询添加序号列

select *,(@number := @number+1) AS number from (select u.mobile,u.`name`, ROUND((select sum(r.realAmount) from t_recharge r where r.userid=u.id and r.status='1' ),2)rechargeSum, ROUND((select sum(w.realAmount) from t_withdrawal w where w.userid=u.id