DataGrid 、Repeater、DataList、GridView自動編號列

來源:互聯網
上載者:User

1、GridView

<asp:TemplateField HeaderText="自增列" FooterText="自增列">
<ItemTemplate>
<%# (Container.DataItemIndex+1).ToString()%>
</ItemTemplate>
</asp:TemplateField>

 2、DataGrid

A.AllowPaging=False情況下,使用一下方法可以實現。

<asp:DataGrid id="DataGrid1" runat="server">    <Columns>    <asp:TemplateColumn>      <ItemTemplate>      <%# Container.ItemIndex + 1%>     </ItemTemplate>     </asp:TemplateColumn>    </Columns> </asp:DataGrid>

  不過更有趣的方法是使用這個方法:

<asp:DataGrid id="DataGrid1" runat="server">    <Columns>    <asp:TemplateColumn>    <ItemTemplate>      <%# this.DataGrid1.Items.Count + 1%>     </ItemTemplate>    </asp:TemplateColumn>   </Columns></asp:DataGrid>

  也許有些人會覺得很奇怪為什麼Items.Count會這樣,而不是出來全部總合,但如果你瞭解綁定的過程時就容易理解。[從上面來看就是在ItemCreated事件中進行綁定所以得到的Items.Count剛好是當前的序號]

 

B、AllowPaging="True"下,如果DataGrid支援分頁則可以如下:

 

<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">   <Columns>     <asp:TemplateColumn>      <ItemTemplate>      <%# this.DataGrid1.CurrentPageIndex * this.DataGrid1.PageSize + Container.ItemIndex + 1%>     </ItemTemplate>   </asp:TemplateColumn>    </Columns></asp:DataGrid>

 

  

 

內容
1 Taye
2 BOx
3 Glass
4 StarCraft

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.