GridView序號問題
http://www.cnblogs.com/aijun/archive/2011/03/15/1984563.html
GridView序號問題
GridView控件中加自動序號,有多種實(shí)現(xiàn)方法,你只需要根據(jù)的實(shí)用要求來確定。總的來分為后臺寫法和前臺寫法,后臺寫法一般不考慮分頁的情況下使用,原理就是在GridView 綁定數(shù)據(jù)時,在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); } } 頁面直接實(shí)現(xiàn)比如直觀,知道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> |
posted on 2011-05-31 10:31 chenlh 閱讀(166) 評論(0) 編輯 收藏 所屬分類: .NET