Google

2008年8月1日 星期五

Girdview 中加入 列序號



我們常常需要在 Gridview 中 加上簡單的 列序號 (顯示 1 2 3 ...)但卻沒辦法簡單的加入,
書上也似乎覺得不需要 也很少人提 (大概是我看太少了吧! ) 不過最近看到一些程式,從裡面學到一些物件,發現原來其實也不會太難啦! 加入 templateFiled 在放上 label 物件 在 databind 中加入
Container.DataItemIndex + 1 就可以了


<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text="<%# Container.DataItemIndex + 1
%>"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField ......

留下紀錄給自己參考
=========================
網路上 找到其他方法 : http://www.wretch.cc/blog/karro81290/12135247

在GridView的RowCreated method
加入下面程式碼
If e.Row.RowIndex >= 0 Then
e.Row.Cells(0).Text = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1
End If

沒有留言: