Skip to main content

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="LastName" HeaderText="Name" />
<asp:BoundField DataField="Lectures" HeaderText="Lectures" />
<asp:TemplateField HeaderText="S.No">

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# (GridView1.PageSize * GridView1.PageIndex) + Container.DisplayIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>



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();
GridView1.DataBind();

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

GridView1.DataSource = GetDataTable();
GridView1.DataBind();
}
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;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr["LastName"] = "Anders";
dr["Lectures"] = 3;
dt.Rows.Add(dr);

}
}

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 %>'


Comments

  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

    ReplyDelete

Post a Comment

Popular posts from this blog