Click here to Skip to main content
15,903,793 members
Articles / Database Development / SQL Server
Article

PagerPanel

Rate me:
Please Sign up or sign in to vote.
2.45/5 (4 votes)
27 Jul 20053 min read 34.4K   470   38   5
Simple data paging control for Windows Forms.

Image 1

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.

C#
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.

C#
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:

C#
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


Written By
Web Developer
Costa Rica Costa Rica
Ing. Luis Alberto Ruiz Arauz
Software Engineering / Industrial Engineering

Comments and Discussions

 
GeneralSQL PROC and Code doesn't match Pin
TheDarkMan27-Jul-05 21:46
TheDarkMan27-Jul-05 21:46 
The stored procedure doesn't match with the criteria in you code

Did you test it ?
GeneralRe: SQL PROC and Code doesn't match Pin
machocr28-Jul-05 5:03
machocr28-Jul-05 5:03 
GeneralRe: SQL PROC and Code doesn't match Pin
TheDarkMan30-Jul-05 10:52
TheDarkMan30-Jul-05 10:52 
GeneralRe: SQL PROC and Code doesn't match Pin
SancarBEY31-Aug-07 1:50
SancarBEY31-Aug-07 1:50 
QuestionWhat data pagind method do you use? Pin
machocr27-Jul-05 9:50
machocr27-Jul-05 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.