Create Auto Generate Serial No in GridView Asp.Net C# VB.Net

In many Asp.net forums i found the question “How to generate a column as serial number at runtime of a gridview?“. Also they have another concern was paging. Thats why here i want to share a simple code snippet with all. Which will help you to add a autogenerate number or sequential number or serial number in your GridView with less effort. Mind it the serial number will not come from Database. We will generate automatically in runtime or in design time. No extra server side code is required.

Objective/Outcome:
GridView_Auto_Generate_Serial_number_Csharp_VB.Net

To do that just add a GridView control in your aspx page. Then create a TemplateField column in the GridView and add Container.DisplayIndex+1 as inline code. This magic line code Will generate your required serial number.

Look at the Code Sample:

<b>List of suppliers:</b>

<asp:GridView runat="server" ID="gvEdit" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
    <Columns>

        <asp:TemplateField HeaderText="S.No">
        <ItemTemplate>
        <%# Container.DisplayIndex + 1 %>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Code" HeaderText="Code">
        </asp:BoundField>
        <asp:BoundField DataField="Name" HeaderText="Name">
        </asp:BoundField>
        <asp:BoundField DataField="Address" HeaderText="Address">
        </asp:BoundField>
        <asp:BoundField DataField="Contact" HeaderText="Contact no">
        </asp:BoundField>
    </Columns>
</asp:GridView>

Populate or Bind GridView with sample Data to Test:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("Code"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            gvEdit.DataSource = dtSupplier;
            gvEdit.DataBind();
        }
    }

Note: Don’t forget to add “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As DataTable = New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("Code"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")

            gvEdit.DataSource = dtSupplier
            gvEdit.DataBind()
        End If
    End Sub

Note: Don’t forget to add “System.Data” namespace.

Now run your application. Hope you will get the Serial Number as a first column in your aspx page. No Database connectivity required here, I have used DataTable to ease the example.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*