Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

PagerPanel

0.00/5 (No votes)
27 Jul 2005 2  
Simple data paging control for Windows Forms.

Introduction

Often is necessary to show users big list of tables or views in database. But doing this can cause a saturation of the network resources (in a distributed environment) and collapse of the user interface. Data paging can be solution to this. It allows you to show the totality of data in a segmented and ordered form, making better use of the memory, network resources and improving the response of user interface.

PagerPanel is a simple user control that allows us to implement data paging in any list control of Windows Forms. PagerPanel doesn�t make queries in database; it only shows the corresponding pages and generates a mechanism to access them.

Requirements

In order to use to PagerPanel we must have the following two things:

  1. We need a stored procedure or method that allows us to make paging queries in database, where we can define the page that we want to access and the size of the page (amount of rows to be shown in each page). Jasmin Muharemovic makes an excellent analysis of different stored procedures methodologies for data paging in this article.
  2. We also need a query that provides information about the total number of pages in the query result. It is very important for generating the list of pages in the control.

Using the control

PagerPanel can be added to the Toolbox of IDE and placed in the form in design time. But the pages are drawn only at run time. In design time an edge is drawn to allow us to visualize its position.

Private void PagerPanel_Paint(object sender, 
             System.Windows.Forms.PaintEventArgs e)
 {
  if(DesignMode)
  {
    Rectangle borderRect = 
       new Rectangle(this.ClientRectangle.Location, 
                          this.ClientRectangle.Size);
    borderRect.Width -= 1;
    borderRect.Height -= 1;
    e.Graphics.DrawRectangle(SystemPens.ControlDark, 
                                         borderRect);
  }

The method CreaNavegacion generates the list of pages returned by the query. This method has two parameters totalPages(int) and currentPage(int). The parameter totalPages represents the total number of pages returned by the query, which is obtained in the query itself. The parameter currentPage is the page that we want to show. In our example, it is used to initiate a new query.

private void btnSearch_Click(object sender, 
                               System.EventArgs e)
 {
  ...
  this.panel.CreaNavigacion(Busca(1),1);
 }

The method Busca is my form is used to obtain the query of a specific page and show it in the DataGrid control, in addition, it gives back the number of pages returned by the query, necessary for the generation of pages in the PagerPanel.

The second step is to implement the OnPanelLinkClicked event, this is executed when the user clicks any Link or page that the control creates. This event provides us the selected page through the argument and is the page that we must query in the database. In our example, we do it in the following way:

private void OnPanelLinkClicked(object sender,
   PagerPanel.PagerPanel.PanelLinkClickedEventArgs e)
{
  this.panel.TotalPages = Busca(e.Page);
}

Note: To run the demo project you must compile the spDataPaging stored procedure script that comes with the source.

Points of interest

The PageBuffer property allows us to define the number of pages the panel will show at each time. If PageBuffer is less than the number of pages in the query results, this symbol << < > >> will appear for navigation of the pages in the list. The default value of this property is 10 but it can be modified according to our requirements. In addition LinkSpace property allows us to define the space between each Link of the navigator.

LinkColor property defines the color of the Link or page that appears in the control.

I hope this control would be useful to you. Feel free to customize it and use it in your projects.

History

  • 2005/07/27 - 1.0.0.1 - Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here