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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
RanjithKumar chiguruvadaPosted Sep 18, 2012, 5:26 AM
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.RanjithKumar chiguruvadaPosted Sep 18, 2012, 9:21 AM
Thank you for accepting my answer.
Satyapriya NayakPosted Sep 18, 2012, 9:12 AM
<%@ 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
nallyaPosted Sep 18, 2012, 5:34 AM
nallyaPosted Sep 18, 2012, 5:20 AM
'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?)
R ACHUTHAPosted Sep 18, 2012, 4:47 AM
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();