How to do Paging
How to do paging in grid view in asp.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
RanjithKumar chiguruvada133 reputationPosted Sep 18, 2012, 1:26 AM EDT
In pge index changing event write the following code
Set
allowpagingproperty of grid view totrueand set
pagesizeproperty to no of records you want in each page.R ACHUTHA181 reputationPosted Sep 18, 2012, 12:47 AM EDT
select gridview ->
goto properties window ->click on events button
double click on pageindexchanging event
and set
allowpaging=true and
set pagesize= some value(No of records do u need to display in the gridview per page)
and write the below code in page index changing event
Gridview.pageIndex=e.NewEditIndex;
BindGrid();
nallyaRank #8,588 · 219 reputationPosted Sep 18, 2012, 1:20 AM EDT
'System.Web.UI.WebControls.GridViewPageEventArgs' does not contain a definition for 'NewEditIndex' and no extension method 'NewEditIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewPageEventArgs' could be found (are you missing a using directive or an assembly reference?)
nallyaRank #8,588 · 219 reputationPosted Sep 18, 2012, 1:34 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 18, 2012, 5:12 AM EDT
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Paging_in_GridView._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Paging_in_GridView
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
}
void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
g1.DataMember = "employee";
g1.DataSource = ds;
g1.DataBind();
con.Close();
}
protected void g1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
g1.PageIndex = e.NewPageIndex;
bindgrid();
}
}
}
Thanks
RanjithKumar chiguruvada133 reputationPosted Sep 18, 2012, 5:21 AM EDT
Thank you for accepting my answer.