Generating
Sequence Number in DataGrid with Paging
In my previous article( Generating Sequence Number in DataGrid) ,
i have explained how to generate sequence number (row
number) in a DataGrid. This method can be easily implemented in
DataGrid if there is no paging involved in that DataGrid. But same
wont work in DataGrid, if paging is also there in DataGrid. To know
how to do paging in DataGrid, refer this article Paging in DataGrid
If you implement this method, in the all the
pages Sequence number will start from one only. But our requirement
is not that, sequence number has to continue in all the pages.
For example if i am going to second page with
Pagesize 10, then sequence number should start from 11.
To achieve this result we need to slightly tweak our previous
method. Code snippet for implementing this method
is,
<asp:DataGrid id="DataGrid1"
runat="server" PagerStyle-Mode=NumericPages PageSize="10"
AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:templatecolumn headertext="Row
Number">
<itemtemplate>
<%# (DataGrid1.PageSize*DataGrid1.CurrentPageIndex)+
Container.ItemIndex+1%>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn runat="server" DataField="CompanyName"
HeaderText="Company
Name">
</asp:boundcolumn>
<asp:boundcolumn runat="server" DataField="Address"
HeaderText="Address">
</asp:boundcolumn>
</Columns>
</asp:DataGrid>
Tweak in the code is very simple, instead
of only using container.itemindex to get the sequence number. You need to
use Pagesize and currentpageindex to find out the starting number of that
page and then add container.itemindex to that. Then your sequence number will
look like the following fig for second page,

|