Top Ad unit 728 × 90

Latest

random

Auto Increment Serial Number in GridView - ASP.Net C# Example

Auto Increment in GridView ASP.Net

This blog post demonstrates how to add serial number in gridview asp.net c#. Sometimes you might need to display a column of serial number for GridView rows and you want it to be auto increment as number of rows increases. There are two different and simple ways to display row numbers in asp.net data controls. Find the code snippet below :-

GridView Auto Increment Column
First you can auto increment using Container.DataItemIndex in GridView :-
    <asp:TemplateField HeaderText="Serial No">                   
                        <ItemTemplate>
                            <%#Container.DataItemIndex + 1 %>
                        </ItemTemplate>
    </asp:TemplateField>
    Auto Increment in GridView ASP.Net,add serial number in gridview,serial number in gridview in asp.net c#
    gridview auto increment column

    Second you can add below 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>

      Repeater

      <asp:Repeater ID="Repeater1" runat="server">
                      <ItemTemplate>
                          <%#Container.ItemIndex+1 %>
                      </ItemTemplate>
      </asp:Repeater>

      DataList
                  <asp:DataList ID="DataList1" runat="server">
                      <ItemTemplate>
                          <%#Container.ItemIndex+1 %>
                      </ItemTemplate>
                  </asp:DataList>

      DetailsView

      <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
                      <Fields>
                          <asp:TemplateField>
                              <ItemTemplate>
                                  <%#Container.DataItemIndex+1 %>
                              </ItemTemplate>
                          </asp:TemplateField>
                      </Fields>
                  </asp:DetailsView>

      FormView

      <asp:FormView ID="FormView1" runat="server">
                      <ItemTemplate>
                          <%#Container.DataItemIndex+1 %>
                      </ItemTemplate>
                  </asp:FormView>

      ListView

      <asp:ListView ID="ListView1" runat="server">
                      <ItemTemplate>
                          <%#Container.DataItemIndex+1 %>
                      </ItemTemplate>
                  </asp:ListView>

      No comments:

      All Rights Reserved by Etechpulse © 2012 - 2016
      Powered By Blogger

      Contact Form

      Name

      Email *

      Message *

      Powered by Blogger.