Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Easy way to add Serial Numbers Column in Gridview

3.54/5 (12 votes)
24 Mar 2010CPOL 39.4K  
May occasions we need to give Serial numbers to our grid like 1,2,3,4,5, this I have tried my hand for a while and what I found.....I have posted it in someones questions earlier. We can give alphabets also but its usage is limited so that part is omitted. (it is somewhat tedious...

May occasions we need to give Serial numbers to our grid like 1,2,3,4,5, this I have tried my hand for a while and what I found.....


I have posted it in someones questions earlier. We can give alphabets also but its usage is limited so that part is omitted. (it is somewhat tedious also).


ASP.NET
<asp:TemplateField>
            <HeaderTemplate>
            Serial No.</HeaderTemplate>
            <ItemTemplate>
            <asp:Label ID="lblSRNO" runat="server" 
                Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

This code works as same when we apply paging.


We cannot use this on BoundFields because they support only those objects who have Databinding event

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)