在vs2005中当需要在GridView中增加一个自动增加序号的列,具体的方法如下:
在<Column >与</column>中加入下面代码(模版列)
方法一:
<asp:TemplateField HeaderText="序号" InsertVisible="False" SortExpression="LessonID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Container.DataItemIndex+1%>' >
</asp:Label>
方法二:
<asp:TemplateField HeaderText="序号" InsertVisible="False" SortExpression="LessonID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' >
</asp:Label>
这两个方法只需要在前台代码修改,不需要修改后台代码.