有三种实现的方式,

第一种方式,直接在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. 将javascript函数写在Html标签里

    有些时候不想把函数写在script标签里了,想直接在html标签里直接加上js代码,可以这样写: <body onload="javascript:{window.location.h ...

  2. 【06-23】js动画学习笔记01

    <html> <head> <style> * { margin:0; padding:0; } #div1{ width:200px; height:200px; ...

  3. SQL实现类似于自动刷新数据的功能

    有时需要在SQL中,定时刷新某张表,比如说是要定时查询某张表的行数,通常做法就是手动的按F5去执行来刷新数据.但是如果这个定时查询历时较长,10分钟,或半小时,手动的话肯定是要崩溃了.貌似SQL没有像 ...

  4. 移动端框架篇-控制子容器的滑屏框架-fullPage.js

    控制子容器法 方法是只显示其中一个子元素,其它隐藏,滑屏时隐藏当前元素,并显示当前元素的下一个同辈元素~ 这里采用fullPage框架,库大小7.69K~ fullPage框架的页面样式无需自定义,已 ...

  5. R语言——七月

    这两个月没有写什么代码.也没做什么大项目,基本就是对以前写的那个用ggplot2可视化数据的项目做一些增增补补,大部分技术难关都在ggplot2和R语言EXCEL处理这里解决并总结了.然后业余帮人修改 ...

  6. JavaScript学习链接

    Js中this的用法:http://www.cnblogs.com/RitaRichard/archive/2011/10/14/2212161.html JavaScript\ActionScrip ...

  7. [MapReduce] Google三驾马车:GFS、MapReduce和Bigtable

    声明:此文转载自博客开发团队的博客,尊重原创工作.该文适合学分布式系统之前,作为背景介绍来读. 谈到分布式系统,就不得不提Google的三驾马车:Google FS[1],MapReduce[2],B ...

  8. Ubuntu 14 中,SecureCRT、SecureFX个性化设置

    [SecureCRT 个性化设置] 打开设置路径:菜单栏 -> Opions -> Global Options -> General -> Default Session - ...

  9. CSS控制表格(table)样式

    CSS控制表格样式 /* 合并边框重叠部分 */ table{border-collapse:collapse;} /* 单元格边框 */ td{border:1px solid #A7AEB1;}

  10. Android 双击 Back 键退出程序

    双击退出程序的原理无非就是设置一个退出标识(询问是否退出),如果改变了这个标识(确认退出),则再次点击时立马退出,如果短时间内没有退出,则延时重置这个标识(不退出). ================ ...