In this article I will explain how to add and show Serial number in First Column of GridView in ASP.Net using C# and VB.Net.
	
		The Serial Number can be auto generated in the following two ways.
	
		1. Using DataItemIndex.
	
		2. Using RowDataBound event.
	
		 
	
		 
	
		Database
	
		I have made use of the following table Customers with the schema as follows.
	![Add (Show) Serial Number in First Column of GridView in ASP.Net using C# and VB.Net]() 
	
		 
	
		 
	
		I have already inserted few records in the table.
	![Add (Show) Serial Number in First Column of GridView in ASP.Net using C# and VB.Net]() 
	
		 
	
		
			Note: You can download the database table SQL by clicking the download link below.
		
	 
	
		 
	
		 
	
		1. Using DataItemIndex.
	
		The first technique is using the DataItemIndex property using which we can easily find the Index or 
Serial Number of a GridView Row.
	
		 
	
		HTML Markup
	
		The below HTML Markup consists of a GridView control where the first column consists of a Label control whose Text property is bound to Container.DataItemIndex property.
	
		The DataItemIndex property is a zero based index and hence we need to increment it by one so that we get values starting from one.
	
		
			<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
		
			<Columns>
		
			    <asp:TemplateField HeaderText = "Row Number" ItemStyle-Width="100">
		
			        <ItemTemplate>
		
			            <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
		
			        </ItemTemplate>
		
			    </asp:TemplateField>
		
			    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
		
			    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
		
			</Columns>
		
			</asp:GridView>
	 
	
		 
	
		 
	
		Namespaces
	
		You will need to import the following namespaces.
	
		C#
	
		
			using System.Data;
		
			using System.Data.SqlClient;
		
			using System.Configuration;
	 
	
		 
	
		VB.Net
	
		
			Imports System.Data
		
			Imports System.Data.SqlClient
		
			Imports System.Configuration
	 
	
		 
	
		 
	
		Populating the GridView control
	
		The GridView is populated using the records from the Customers table inside the Page Load event of the ASP.Net page.
	
		C#
	
		
			protected void Page_Load(object sender, EventArgs e)
		
			{
		
			    if (!this.IsPostBack)
		
			    {
		
			        this.BindGrid();
		
			    }
		
			}
		
			 
		
			private void BindGrid()
		
			{
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers"))
		
			        {
		
			            using (SqlDataAdapter sda = new SqlDataAdapter())
		
			            {
		
			                cmd.Connection = con;
		
			                sda.SelectCommand = cmd;
		
			                using (DataTable dt = new DataTable())
		
			                {
		
			                    sda.Fill(dt);
		
			                    GridView1.DataSource = dt;
		
			                    GridView1.DataBind();
		
			                }
		
			            }
		
			        }
		
			    }
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
		
			    If Not Me.IsPostBack Then
		
			        Me.BindGrid()
		
			    End If
		
			End Sub
		
			 
		
			Private Sub BindGrid()
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("SELECT Name, Country FROM Customers")
		
			            Using sda As New SqlDataAdapter()
		
			                cmd.Connection = con
		
			                sda.SelectCommand = cmd
		
			                Using dt As New DataTable()
		
			                    sda.Fill(dt)
		
			                    GridView1.DataSource = dt
		
			                    GridView1.DataBind()
		
			                End Using
		
			            End Using
		
			        End Using
		
			    End Using
		
			End Sub
	 
	
		 
	![Add (Show) Serial Number in First Column of GridView in ASP.Net using C# and VB.Net]() 
	
		 
	
		 
	
		 
	
		 
	
		2. Using RowDataBound event.
	
		The second technique is using the RowDataBound event of the GridView control.
	
		 
	
		HTML Markup
	
		The below HTML Markup consists of a GridView control where the first column consists of a Label control whose Text property will be set inside the assigned RowDataBound event handler.
	
		
			<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
		
			<Columns>
		
			    <asp:TemplateField HeaderText = "Row Number" ItemStyle-Width="100">
		
			        <ItemTemplate>
		
			            <asp:Label ID="lblRowNumber" runat="server" />
		
			        </ItemTemplate>
		
			    </asp:TemplateField>
		
			    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
		
			    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
		
			</Columns>
		
			</asp:GridView>
	 
	
		 
	
		 
	
		Namespaces
	
		You will need to import the following namespaces.
	
		C#
	
		
			using System.Data;
		
			using System.Data.SqlClient;
		
			using System.Configuration;
	 
	
		 
	
		VB.Net
	
		
			Imports System.Data
		
			Imports System.Data.SqlClient
		
			Imports System.Configuration
	 
	
		 
	
		 
	
		Populating the GridView control
	
		The GridView is populated using the records from the Customers table inside the Page Load event of the ASP.Net page.
	
		C#
	
		
			protected void Page_Load(object sender, EventArgs e)
		
			{
		
			    if (!this.IsPostBack)
		
			    {
		
			        this.BindGrid();
		
			    }
		
			}
		
			 
		
			private void BindGrid()
		
			{
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers"))
		
			        {
		
			            using (SqlDataAdapter sda = new SqlDataAdapter())
		
			            {
		
			                cmd.Connection = con;
		
			                sda.SelectCommand = cmd;
		
			                using (DataTable dt = new DataTable())
		
			                {
		
			                    sda.Fill(dt);
		
			                    GridView2.DataSource = dt;
		
			                    GridView2.DataBind();
		
			                }
		
			            }
		
			        }
		
			    }
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
		
			    If Not Me.IsPostBack Then
		
			        Me.BindGrid()
		
			    End If
		
			End Sub
		
			 
		
			Private Sub BindGrid()
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("SELECT Name, Country FROM Customers")
		
			            Using sda As New SqlDataAdapter()
		
			                cmd.Connection = con
		
			                sda.SelectCommand = cmd
		
			                Using dt As New DataTable()
		
			                    sda.Fill(dt)
		
			                    GridView2.DataSource = dt
		
			                    GridView2.DataBind()
		
			                End Using
		
			            End Using
		
			        End Using
		
			    End Using
		
			End Sub
	 
	
		 
	
		 
	
		Displaying Auto generated Row Number in GridView using RowDataBound event 
	
		Inside the RowDataBound event handler, the Label control is searched and then its Text property is set to the RowIndex of the GridView Row after incrementing it by one similar to DataItemIndex property as RowIndex is also a zero based index property.
	
		C#
	
		
			protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
		
			{
		
			    if (e.Row.RowType == DataControlRowType.DataRow)
		
			    {
		
			        (e.Row.FindControl("lblRowNumber") as Label).Text = (e.Row.RowIndex + 1).ToString();
		
			    }
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
		
			    If e.Row.RowType = DataControlRowType.DataRow Then
		
			       TryCast(e.Row.FindControl("lblRowNumber"), Label).Text = (e.Row.RowIndex + 1).ToString()
		
			    End If
		
			End Sub
	 
	
		 
	![Add (Show) Serial Number in First Column of GridView in ASP.Net using C# and VB.Net]() 
	
		 
	
		 
	
		Demo
	
	
		 
	
		 
	
		Downloads