Enabling Paging on a GridView

So I have been recently playing with GridViews in asp.net. Normally I use repeaters but the auto styles and paging have been appealing to me a bit lately. But there is some trickery to enable the paging on a GridView. You have to write a wee bit of code to get it to work correctly. The documentation doesn't make it abundantly clear on how to do this. What you need to do is implement the PageIndexChaning event and rebind your datasource to the grid. So for my project the code looks like this:

protected void GridViewProducts_PageIndexChanging( object sender, GridViewPageEventArgs e )
{
/* Reset your datasource, I am using a custom collection here */
this.GridViewProducts.DataSource = this.ProductsList;

/* Set the new page index this comes from the GridViewPageEventArgs arguement passed into the method */
this.GridViewProducts.PageIndex = e.NewPageIndex;

/* Rebind the grid */
this.GridViewProducts.DataBind();
}

Comments

Popular posts from this blog

String.Replace vs Regex.Replace

C# Form Application in Kiosk Mode/Fullscreen

C# using a transaction with ODBC