經(jīng)典、無碼、o(∩_∩)o...

          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


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 拜泉县| 广昌县| 建水县| 临沭县| 贵定县| 防城港市| 汾阳市| 定边县| 依安县| 伊春市| 宝坻区| 凤山市| 孝义市| 麦盖提县| 定州市| 梓潼县| 万年县| 白朗县| 荆门市| 泸西县| 淮安市| 兴文县| 托克托县| 瑞金市| 沾益县| 马公市| 长阳| 喀喇沁旗| 绥化市| 哈密市| 游戏| 侯马市| 仙居县| 上饶县| 江阴市| 宁城县| 平塘县| 洪泽县| 且末县| 南部县| 长春市|