zoukankan      html  css  js  c++  java
  • GridView,Repeater增加自动序号列

    有三种实现的方式,
    第一种方式,直接在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>
    就可以了。
    <asp:RepeaterID="Repeater1"runat="server">
       <ItemTemplate>
           <%# Container.ItemIndex+1%>
           <%# (ContainerasRepeaterItem).ItemIndex+1%>
       </ItemTemplate>
    </asp:Repeater>
    赠送一个小技巧:
    Repeater数据为空时提示暂无数据
    <FooterTemplate>  
           
    <asp:Label ID="lblEmpty" Text="暂无数据" runat="server"  Visible='<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>'></asp:Label> 
                    
      </FooterTemplate>
  • 相关阅读:
    面向过程, 面向对象, 类和对象, 类与数据类型
    python函数
    简单的登录注册函数
    Java遍历包中所有类
    spring boot jar启动
    过期算法
    负载均衡算法
    spring boot druid mybatis多数据源
    多线程wait和notify实现1212
    多线程售票
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628971.html
Copyright © 2011-2022 走看看