网上有许多关于DataGrid自动生成序号的文章,大体也是通过一个记数函数生成序号,这种技巧在表示层开发中很受用,因为DataGrid是邦定DataTable的,如果通过修改数据层来生序号未免有点大动干戈。可以使用一个自增函数记录序号,在DataGrid中调用次方法就可以实现了:
方法:
1
int count;
2
public int GetCount()
3
{
4
int pageindex=0;
5
++count;
6
if(!IsPostBack)
7
{
8
pageindex=count;
9
}
10
else
11
{
12
if(ItemListData1.CurrentPageIndex>=1)
13
{
14
15
pageindex=count+(ItemListData1.PageSize*ItemListData1.CurrentPageIndex);
16
}
17
if(ItemListData1.CurrentPageIndex==0)
18
{
19
pageindex=count-ItemListData1.PageSize;
20
}
21
}
22
return pageindex;
23![]()
24
}
调用方法:
int count;2
public int GetCount()3
{4
int pageindex=0;5
++count;6
if(!IsPostBack)7
{8
pageindex=count;9
}10
else11
{12
if(ItemListData1.CurrentPageIndex>=1)13
{14
15
pageindex=count+(ItemListData1.PageSize*ItemListData1.CurrentPageIndex);16
}17
if(ItemListData1.CurrentPageIndex==0)18
{19
pageindex=count-ItemListData1.PageSize;20
}21
}22
return pageindex;23

24
}<asp:TemplateColumn HeaderText="序号">
<ItemTemplate>
<asp:label id="lable1" runat=server><%#GetCount()%></asp:label>
</ItemTemplate>
</asp:TemplateColumn>
在此增强了对postback情况的处理!
参考:http://blog.csdn.net/yx10/archive/2005/07/08/417481.aspx

浙公网安备 33010602011771号