Asp.net - Auto generate serial no in GridView


Sometimes we have a scenario to create a column that will auto generate the serial number, so in this post I will explain how to generate serial number in Grid View.
  •   You can do using Container.DataItemIndex in GridView
<asp:TemplateField HeaderText="Serial No">                   
                    <ItemTemplate>
                        <%#Container.DataItemIndex + 1 %>
                    </ItemTemplate>
</asp:TemplateField>


C# - Simple ways to Auto Generate Serial Number in GridView - An Example
  • You can add following line of code to auto generate a column in GridView
  <asp:TemplateField HeaderText="Serial No">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"
                        Text='<%# (GridView1.PageSize * GridView1.PageIndex) + Container.DisplayIndex + 1 %>'>
                        </asp:Label>
                    </ItemTemplate>
</asp:TemplateField>

Download the source code click here ..

Post a Comment

 
Top