SiteMap  | About    

Auto Generate Row Number in GridView

Gridview row index number

The GridView control is the successor to the DataGrid and extends it in a number of ways. While developing GridView control in ASP.NET, programmers often requires to display row number in GridView controls.

row-number in gridview


In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.


Auto Generate Row Number in GridView

It is a common requirement that GridViews to have the first column just be a number identifying the row. Because of doing this implementation, the user can see the number of rows returned by scrolling to the end of the GridView. By adding a single piece of code you can add a row number column to your GridView Control.

row-index in gridview

The following source code show how to create a row number column field to your Asp.Net GridView



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#BFE4FF" />
                    <asp:TemplateField HeaderText="No.">
                            <%# Container.DataItemIndex + 1 %>
					<asp:BoundField DataField="pub_id" HeaderText="pub_id" />
					<asp:BoundField DataField="pub_name" HeaderText="pub_name" />
					<asp:BoundField DataField="city" HeaderText="city" />
					<asp:BoundField DataField="state" HeaderText="state" />
					<asp:BoundField DataField="country" HeaderText="country" />

C# Source Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        int i = 0;
        string sql = null;
        string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=zen412";
        sql = "select * from publishers";
        SqlConnection connection = new SqlConnection(connetionString);
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        GridView1.DataSource = ds.Tables[0];

VB.Net Source Code


Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim adapter As New SqlDataAdapter()
        Dim ds As New DataSet()
        Dim i As Integer = 0
        Dim sql As String = Nothing
        Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=zen412"
        sql = "select * from publishers"
        Dim connection As New SqlConnection(connetionString)
        Dim command As New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        GridView1.DataSource = ds.Tables(0)
    End Sub
End Class (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.