23 November 2013

How to display serial number automatically in GridView in asp.net?

In this post we will discuss how to display a serial number column in gridview in asp.net.

Serial number will come as an index, it will start from 1 and will go on increasing 1 value for each row.

Here the code to bind a gridview will be same. Only thing will change is we need to another <ItemTemplate> like below:

<asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="Serial Number">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

If you will write this code then the serial number column will come as like shown below...




No comments:

Post a Comment