Display Page count in ASP.NET GridView

Continuing my ASP.NET GridView Tips and Tricks series, this post shows how to display the page count in an ASP.NET GridView control

In one of our previous articles, we saw how to Highlight the current page in an ASP.NET GridView pager. Let us extend the example, to show the page count too.

The first step is to set the ShowFooter property to true and add a RowDataBound event to the GridView, as shown below

image

Next write the following code in the GridView_RowDataBound event

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = (GridView1.PageIndex + 1) + " of "
+ GridView1.PageCount;
}
}

That’s it. View the GridView control and you should see the page count visible as shown below

image



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

Anonymous said...

Very good article. Keep up the good work.

kk said...

ok nice