Gridview add / show srno : How to add or show auto-generated incremented serial number in gridview

/ / 0 Comments

Overview: Adding an auto-generated serial number, show the first column as Sr no in Gridview asp.net.  This article will explain how to add or show auto increment the serial number.i.e. add or show auto-generated serial no (Sr no) as the first column in gridview control.

Serial number Sr no can be added by using DataItemIndex in Gridview control.

DataItemIndex: Gets the index of the DataItem in the underlying DataSet.

Code::

Using DataItemIndex, i.e  By adding code inside gridview you will get an auto-generated serial number. You may notice in the below code we had incremented  DataItemIndex by one ( 1 ), because the DataItemIndex property is the zero-based index, so to starts from one 1, we incremented it by one.

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

Finally, our code  Gridview will look like this, where the first column of gridview is an auto-generated serial number.

//*
<asp:GridView ID="gvAddSerialNumber" runat="server">
    <Columns>
        <asp:TemplateField  HeaderText="Sr. no">
            <ItemTemplate>
                <%#Container.DataItemIndex+1 %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
//*


Yup, now we are done with auto-generated incremented serial number ;-)

You must check these articles:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments