有三种实现的方式,

第一种方式,直接在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()方法添加代码,最后代码如下所示:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 //清清月儿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>

就可以了。

%>%>
    </ItemTemplate>
</asp:Repeater>

赠送一个小技巧:

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

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

GridView,Repeater增加自动序号列的更多相关文章

  1. aspxgridView,Repeater增加自动序号列

    第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. <asp:TemplateField HeaderText="序号" Ins ...

  2. ASP.NET(C#)--Repeater中生成“序号”列

    需求介绍:在Repeater(Table)中加入“序号”列,从1开始自增,步长为1. 思路:因为“序号”跟Repeater的行号有关,所以要在Repeater的ItemDataBound事件中输出“序 ...

  3. GridView,datalist添加序号列

    GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...

  4. DataList分页-增加自动编号列

    <asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...

  5. Oracle使用row_number()函数查询时增加序号列

    使用Oracle自带的row_number()函数能够实现自动增加序号列的要求,但是同时引发一个问题,如果我们查询出来的数据需要使用Order By排序的话,那么我们会发现新增加的序号列是乱序的,它会 ...

  6. .Net GridView 序号列

    给GridView增加一列:序号列 <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# (( ...

  7. ASP.NET 为GridView添加序号列,且支持分页连续累计显示

    为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码: <%@ Page Language="C#" AutoEventWireup="tr ...

  8. 使用模板技术处理ASP.NET中GridView额外序号列的问题

    问题描述: 现在要在一张GridView表中添加一列来显示行号,要求是显示下一页的时候能够递增而不是从新编号.数据库中的没有相关序号列 如何在软件端实现呢? 通过测试,添加以下代码即可解决需求. &l ...

  9. ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##

    一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...

随机推荐

  1. CSS3中的动画功能(一)

    css3中的动画功能分为transitions功能和animations功能,这两种功能都可以通过改变css属性值来产生动画效果.今天带大家一起来看看css3动画功能中的transitions的用法. ...

  2. [转] 64位windows下添加postgreSQL odbc数据源

    系统环境:windows7 64位 postgreSQL9.0(64bit)   ps:安装postgreSQL时确定安装了odbc驱动.   问题:点击“开始->控制面板->管理工具-& ...

  3. [NHibernate]条件查询Criteria Query

    目录 写在前面 文档与系列文章 条件查询 一个例子 总结 写在前面 上篇文章介绍了HQL查询,我个人觉得使用ORM框架就是为了让少些sql,提高开发效率,而再使用HQL就好像还是使用sql,就觉得有点 ...

  4. hzwer模拟赛 感冒病毒

    题目描述 Description 一种感冒病毒正在学校里传播,这所学校有n个学生,m个学生社团,每个学生可能参加了多个社团,因为同一个社团的学生交流较多,所以如果一个学生感染上感冒病毒,那么他所在的社 ...

  5. svn 版本转为git

    git clone 相当于git init 和 git svn fetch.git svn rease git svn fetch 从svn服务器取指定区间的版本转化成git库 git svn reb ...

  6. Windows下,MySQL root用户忘记密码解决方案

    同时打开2个命令行窗口,并按如下操作: <1>.在第一个“命令行窗口”输入: cd  D:\Program Files\MySQL\MySQL Server 5.5\bin net sto ...

  7. Mac Pro 实现 PHP-5.6 与 PHP-7.0 等多版本切换

    先前参考 如何 实现PHP多版本的 共存 和 切换? 实现了Linux(Ubuntu/CentOS)系统下,PHP多版本的切换,但是在 Mac OS 下,由于用户权限控制的比较严格,文章里提到的脚本运 ...

  8. BZOJ2982——combination

    1.题意:求 C(n,m) % 10007 ,10007是质数咯 n和m < 2000000000 2.分析:这个东西太大了,显然不能用n!的阶乘预处理的方式搞出来,也不能用递推公式搞出来 于是 ...

  9. [转]在MyEclipse中设置struts.xml自动提示功能

    导入标签:<%@ taglib uri="/struts-tags" prefix="s" %> 要想在MyEclipse中实现struts.xml ...

  10. ng-bind 与ng-model区别

    双向绑定,一般来说是这样 <input ng-model="object.xxx"> <span ng-bind="object.xxx"&g ...