Monday, May 4, 2009

Gridview Paging

protected void Page_Load(object sender, EventArgs e)
{
//First page records to be downloaded for ordinary request.
if (Page.IsPostBack == false)
{
GridView1.PageIndex = 0;
FillGrid();
//DataBind method of gridview will perform paging.It will send page records to user based up on
//pageindex property value
Label1.Text = "1 out of" + GridView1.PageCount;
}
}

private void FillGrid()
{
SqlConnection con = new SqlConnection("trusted_connection=yes;database=northwind;server=localhost");
SqlDataAdapter da = new SqlDataAdapter("select * from customers", con);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
GridView1.DataSource = ds.Tables["Customers"];
GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//e.NewPageIndex will provide page number to be downloaded to client.
FillGrid();
Label1.Text = e.NewPageIndex + 1 + " Out of " + GridView1.PageCount;
}

No comments:

Post a Comment