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.
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:
- Drag Drop to reorder GridView Rows in Asp.net C#.
- Bind drop-down list inside Gridview edit template.
- Sorting Gridview control on header click with pagination
- Gridview Row background color based on the condition in Asp.net
- Easy way to upload Bulk Image in Asp.net C# using Dropzone JS.
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