ASP.net 2.0 で GridView に連番(通し番号)を振る方法

ASP.net 2.0 で GridView コントロールの行に連番(通し番号)を付ける方法です。

テンプレート列を利用して Container.DataItemIndex プロパティを呼び出します。
Container.DataItemIndex プロパティに入る数値は 0 から始まりますから、1 を起点にするなら +1 してください。

<asp:GridView>
 <Columns>
  <asp:TemplateField>
   <ItemTemplate>
    <%# Container.DataItemIndex + 1  %>
   </ItemTemplate>
  </asp:TemplateField>
 </Columns>
</asp:GridView>

ASP.net 1.1 の場合は @IT の記事が参考になります。
@IT /[ASP.NET]DataGridコントロールの行に通し番号を付けるには?

Published 木, 4 20 2006 11:10 by Ryosuke Uemoto

Comments

# re: ASP.net 2.0 で GridView に連番(通し番号)を振る方法

質問です。<#% Container.DataItemIndex +1%>はSqlDataSource等で、データバインドさせていたら出来たのですが、コードからDataSet等でバインドさせた場合無表示でした。どうしたらいいでしょうか?

2007年4月6日 16:45 by ny

# re: ASP.net 2.0 で GridView に連番(通し番号)を振る方法

私は RowCreated イベントで処理しています。

   Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

       If e.Row.RowIndex >= 0 Then

           e.Row.Cells(0).Text = e.Row.RowIndex + 1

       End If

   End Sub

2007年7月20日 10:58 by Masa

# ASP.net2.0???GridView?????????(????????????)??????????????? - Clare Chronicle

Pingback from  ASP.net2.0???GridView?????????(????????????)??????????????? - Clare Chronicle

# Chrysler Lebaron Tube, 1989 Lebaron Sale

Pingback from  Chrysler Lebaron Tube, 1989 Lebaron Sale

2010年5月22日 19:00 by Chrysler Lebaron Tube, 1989 Lebaron Sale

# 300sd Inexpensive 500sec, 500sec Bulb Nickel Base - 254.cmanager.org

Pingback from  300sd Inexpensive 500sec, 500sec Bulb Nickel Base - 254.cmanager.org

# V3000 Diagram, Terminal Wyse Winterm V30l - 238.defutbolazo.com

Pingback from  V3000 Diagram, Terminal Wyse Winterm V30l - 238.defutbolazo.com