Wednesday, April 30, 2008

How To Display Row Numbers In A GridView Control [ASP.NET]

While developing web sites using ASP.NET, developers often requires to display row number in GridView controls. Here is a simple template field code  to display sequence number start from 1. Insert this code as one of the fields in our GridView code.

<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>



Incase if you wish to start the sequence number from any number other than 1, replace Container.DataItemIndex + 1 with Container.DataItemIndex + n.  For example Container.DataItemIndex + 5 substitution in the above code starts generating sequence number from 5 onwards.


1 Comment:

Anonymous said...

Thanks for the post