Search Code Guru

How to display a serial number from 1 to n in a grid view

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Paging.aspx.cs" Inherits="Paging" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging">
<asp:BoundField DataField="LastName" HeaderText="Name" />
<asp:BoundField DataField="Lectures" HeaderText="Lectures" />
<asp:TemplateField HeaderText="S.No">

<asp:Label ID="Label1" runat="server" Text='<%# (GridView1.PageSize * GridView1.PageIndex) + Container.DisplayIndex + 1 %>'></asp:Label>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Paging : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
GridView1.DataSource = GetDataTable();

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;

GridView1.DataSource = GetDataTable();
private DataTable GetDataTable()
//create table
DataTable dt = new DataTable("Members");
dt.Columns.Add("ID", Type.GetType("System.Int32"));
dt.Columns.Add("LastName", Type.GetType("System.String"));
dt.Columns.Add("Lectures", Type.GetType("System.Int32"));

//create fields
DataColumn[] pk = new DataColumn[1];
pk[0] = dt.Columns["ID"];
dt.PrimaryKey = pk;
dt.Columns["ID"].AutoIncrement = true;
dt.Columns["ID"].AutoIncrementSeed = 1;
dt.Columns["ID"].ReadOnly = true;

//fill rows
DataRow dr;
for (int x = 1; x <= 10; x++)
//make every other one different
if (Math.IEEERemainder(x, 2) == 0)
dr = dt.NewRow();
dr["LastName"] = "Riss";
dr["Lectures"] = 14;
dr = dt.NewRow();
dr["LastName"] = "Anders";
dr["Lectures"] = 3;


return dt;

create a template field, in that item template, put a label, specify it's text as

Text='<%# (GridView1.PageSize * GridView1.PageIndex) + Container.DisplayIndex + 1 %>'

Post a Comment


  1. I am trying to convert complete text between table /table tags. But getting the following error.Control 'ctl00_ContentPlaceHolder1_Basicsal' of type 'TextBox' must be placed inside a form tag with runat=server.
    Where as i put complete front end code within form tag with runat="server".Need Help.Thanks in advance


Please do not post any spam link in the comment box😊