GridView序號

來源:互聯網
上載者:User
GridView控制項中加自動序號,有多種實現方法,你只需要根據的實用要求來確定。總的來分為後台寫法和前台寫法,後台寫法一般不考慮分頁的情況下使用,原理就是在GridView 綁定資料時,在RowDataBound 事件中來處理。

頁面的列為:

<asp:BoundField  HeaderText="序號" />

或用

<asp:TemplateField HeaderText="序號"> <ItemTemplate> </ItemTemplate> </asp:TemplateField> 

CS代碼為:

protected void GridView1_RowDataBond(object sender, GridViewRowEventArgs e){         if (e.Row.RowIndex >= 0)         {          e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);         }              }

頁面直接實現比如直觀,知道Container.DataItemIndex 屬性的含義就行:

<asp:TemplateField HeaderText="序號"> <ItemTemplate>    <%# Container.DataItemIndex + 1%>  </ItemTemplate> </asp:TemplateField>

下面考慮的主要是分頁情況下的,在ASP.NET中分頁方法一般用GridView內建的分頁工具和AspNetPager的比較多。GridView內建的分頁寫法

<asp:TemplateField HeaderText="序號"> <ItemTemplate> <%# this.GridView1.PageIndex  * this.GridView1.PageSize          + GridView1.Rows.Count + 1%> </ItemTemplate> </asp:TemplateField> 

AspNetPager分頁情況下的寫法為

<asp:TemplateField HeaderText="序號"> <ItemTemplate>    <%# (this.Pager1.CurrentPageIndex - 1) * this.Pager1.PageSize     + Container.DataItemIndex + 1%> </ItemTemplate> </asp:TemplateField> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.