GridView序號(hào)問題
http://www.cnblogs.com/aijun/archive/2011/03/15/1984563.html
GridView序號(hào)問題
GridView控件中加自動(dòng)序號(hào),有多種實(shí)現(xiàn)方法,你只需要根據(jù)的實(shí)用要求來確定??偟膩矸譃楹笈_(tái)寫法和前臺(tái)寫法,后臺(tái)寫法一般不考慮分頁的情況下使用,原理就是在GridView 綁定數(shù)據(jù)時(shí),在RowDataBound 事件中來處理。 頁面的列為: <asp:BoundField HeaderText="序號(hào)" />
或用 <asp:TemplateField HeaderText="序號(hào)">
<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); } } 頁面直接實(shí)現(xiàn)比如直觀,知道Container.DataItemIndex 屬性的含義就行: <asp:TemplateField HeaderText="序號(hào)">
<ItemTemplate> <%# Container.DataItemIndex + 1%> </ItemTemplate> </asp:TemplateField> 下面考慮的主要是分頁情況下的,在ASP.NET中分頁方法一般用GridView自帶的分頁工具和AspNetPager的比較多。GridView自帶的分頁寫法: <asp:TemplateField HeaderText="序號(hào)">
<ItemTemplate> <%# this.GridView1.PageIndex * this.GridView1.PageSize + GridView1.Rows.Count + 1%> </ItemTemplate> </asp:TemplateField> AspNetPager分頁情況下的寫法為: <asp:TemplateField HeaderText="序號(hào)">
<ItemTemplate> <%# (this.Pager1.CurrentPageIndex - 1) * this.Pager1.PageSize + Container.DataItemIndex + 1%> </ItemTemplate> </asp:TemplateField> |
posted on 2011-05-31 10:31 chenlh 閱讀(168) 評(píng)論(0) 編輯 收藏 所屬分類: .NET