Custom PageSize of GridView in Runtime

As I have previously talked, GridView is the most used data related control on Microsoft ASP.NET platform. We have previously written many articles on GridView excluding Custom Pagesize. In case you want to read, few of them are below.

Searching GridView Data in ASP.NET , Print GridView only in ASP.NETExport to Word from GridView in ASP.NETCustom Message when no rows in GridViewGridView with Fixed Header and ScrollbarExport to Excel from DataGridView in ASP.NETPagination in GridView and many other.

Custom PageSize Introduction:

Today, I am sharing one more helpful code snippet related to GridView in ASP.NET. GridView are used to display records from database or external datasource and are replacement of Static HTML tables which can’t work with data bound controls like GridView.

Need Of Custom Pagesize in GridView:

When the data is large in nature and we need to display it in small view, we use paging feature of GridView and achieve Custom Pagesize.

Paging allows us to load large data quickly as well as make rendered HTML page of small size which make website load faster.

But in some cases where user might want to change pagesize at their own convenience. Typically this is needed when our general configured screen-size and display resolution and size of user is not equal, not even close to near. Lets be clear with below example.

I have developed a application considering for screen resolution of 1366×768 but one of my user accessed application using a Full HD (1920×1080) display which caused mess with GridView size.

So, I implemented some changes in that GridView control which helped me to make application more useful as well as more user friendly and environment adaptable.

Design of data GridView before implementing Custom Pageview:

Lets be clear with a design followed by its source code.

Custom Page Size

Source code for current view:

Code for Custom Pagesize of GridView:

We have added a new row and two TextBox in above design to put Page Size and Page Number. In this design you can enter the size and page number of the view. Lets see the important section of code in above source code.

In above two TextBox, we set their AutoPostBack property to true and coded some event in TextChanged Event of both TextBox.

Design of data after implementing Custom Pagesize in GridView:

Below is the design with these two TextBox.

Custom PageSize of GridView


Let me share the code for both TextChanged property.

Final view after Custom Pagesize:

Main features of above design and code will be like below.

Custom PageSize of GridView

Description of output:

  1. The Number entered in Custom Pagesize field, GridView PageSize property will be updated with the above value and page will refresh to load GridView in with new settings.
  2. Similarly, Page no field will change the current page index of gridview to value entered in GridView.
  3. No need to hit Enter or press any button, TextChanged command will be automatically executed due to PostBack nature of TextBox object.

In this article, we have learnt how to make a Custom Pagesize gridview which can change hard coded value from user input to make it more useful and fancy.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *