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

          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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 日喀则市| 依兰县| 奎屯市| 湟源县| 海丰县| 德清县| 寿宁县| 宜君县| 基隆市| 张家港市| 建宁县| 沅陵县| 苍梧县| 资溪县| 平果县| 鲁山县| 建湖县| 宁城县| 紫阳县| 错那县| 榆中县| 临邑县| 灵丘县| 喜德县| 鄂托克旗| 东丽区| 高青县| 竹北市| 烟台市| 芒康县| 会同县| 乌海市| 荣昌县| 时尚| 夏邑县| 蓬安县| 澄江县| 嘉义市| 门头沟区| 松原市| 铁岭市|