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 below code we had incremented DataItemIndex by one ( 1 ), because 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 increamented 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.
Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!