Some times it is important to show the Serial No. for each row of the databound controls like gridview or datagrind in Asp.net And i found people used to do it by taking a global variable and incrementing it on databound or things like this.But the solutions i am going to give you is pretty staight forwand and doesn't need and code behind or Row data bound event. So below is the code that you can uer to generate the serial number.
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
</asp:GridView>
The above Example will generate the serial number for each row.
Now the question is how to generate the serial number in Gridview having paging enabled. So the below example is the answer to that question.
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" PageSize="10" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%# (gvUsers.PageSize*gvUsers.PageIndex)+ Container.DataItemIndex+1 %></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
</asp:GridView>
|