Custom winform paging Control

Source: Internet
Author: User

Address: http://www.cnblogs.com/nosnowwolf/archive/2008/07/22/1248796.html

Http://blog.csdn.net/wanglong7505/archive/2010/01/16/5193085.aspx

The effect is as follows:

CodeThe implementation is as follows:

Namespace windowsapp. mycontrol
{
/** // <Summary>
/// Declare Delegation
/// </Summary>
/// <Param name = "E"> </param>
/// <Returns> </returns>
Public Delegate int eventpaginghandler (eventpagingarg E );
/** // <Summary>
/// Display the paging Control
/// </Summary>
Public partial class Pager: usercontrol
{
Public Pager ()
{
Initializecomponent ();
}
Public event eventpaginghandler eventpaging;
/** // <Summary>
/// Number of records displayed per page
/// </Summary>
Private int _ pagesize = 20;
/** // <Summary>
/// Number of records displayed per page
/// </Summary>
Public int pagesize
{
Get {return _ pagesize ;}
Set
{
_ Pagesize = value;
Getpagecount ();
}
}

Private int _ Nmax = 0;
/** // <Summary>
/// Total number of records
/// </Summary>
Public int Nmax
{
Get {return _ Nmax ;}
Set
{
_ Nmax = value;
Getpagecount ();
}
}

Private int _ pagecount = 0;
/** // <Summary>
/// Page number = Total number of records/number of records displayed per page
/// </Summary>
Public int pagecount
{
Get {return _ pagecount ;}
Set {_ pagecount = value ;}
}

Private int _ pagecurrent = 0;
/** // <Summary>
/// Current page number
/// </Summary>
Public int pagecurrent
{
Get {return _ pagecurrent ;}
Set {_ pagecurrent = value ;}
}


Private void getpagecount ()
{
If (this. Nmax> 0)
{
This. pagecount = convert. toint32 (math. Ceiling (convert. todouble (this. Nmax)/convert. todouble (this. pagesize )));
}
Else
{
This. pagecount = 0;
}
}

/** // <Summary>
/// How to bind the paging Control Data
/// </Summary>
Public void BIND ()
{
If (this. eventpaging! = NULL)
{
This. Nmax = This. eventpaging (New eventpagingarg (this. pagecurrent ));
}

If (this. pagecurrent> This. pagecount)
{
This. pagecurrent = This. pagecount;
}
If (this. pagecount = 1)
{
This. pagecurrent = 1;
}
Lblpagecount. Text = This. pagecount. tostring ();
This. lblmaxpage. Text = "Total" + this. Nmax. tostring () + "record ";
This.txt currentpage. Text = This. pagecurrent. tostring ();

If (this. pagecurrent = 1)
{
This. btnprev. Enabled = false;
This. btnfirst. Enabled = false;
}
Else
{
Btnprev. Enabled = true;
Btnfirst. Enabled = true;
}

If (this. pagecurrent = This. pagecount)
{
This. btnlast. Enabled = false;
This. btnnext. Enabled = false;
}
Else
{
Btnlast. Enabled = true;
Btnnext. Enabled = true;
}

If (this. Nmax = 0)
{
Btnnext. Enabled = false;
Btnlast. Enabled = false;
Btnfirst. Enabled = false;
Btnprev. Enabled = false;
}
}

Private void btnfirst_click (Object sender, eventargs E)
{
Pagecurrent = 1;
This. BIND ();
}

Private void btnprev_click (Object sender, eventargs E)
{
Pagecurrent-= 1;
If (pagecurrent <= 0)
{
Pagecurrent = 1;
}
This. BIND ();
}

Private void btnnext_click (Object sender, eventargs E)
{
This. pagecurrent + = 1;
If (pagecurrent> pagecount)
{
Pagecurrent = pagecount;
}
This. BIND ();
}

Private void btnlast_click (Object sender, eventargs E)
{
Pagecurrent = pagecount;
This. BIND ();
}

Private void btngo_click (Object sender, eventargs E)
{
If (this.txt currentpage. Text! = NULL & txtcurrentpage. Text! = "")
{
If (int32.tryparse (txtcurrentpage. Text, out _ pagecurrent ))
{
This. BIND ();
}
Else
{
Common. messageprocess. showerror ("incorrect number format! ");
}
}
}

}
/** // <Summary>
/// Custom Event Data Base Class
/// </Summary>
Public class eventpagingarg: eventargs
{
Private int _ intpageindex;
Public eventpagingarg (INT pageindex)
{
_ Intpageindex = pageindex;
}
}
}

Controls.

How to bind data?

A large number of pages use stored procedures.

This stored procedure was tested online. I will paste it out, hoping that the original author will not hit me with bricks .....




Alter procedure sp_pagination
/**//*
**************************************** ***********************
** Tens of millions of paging stored procedures **
**************************************** ***********************
Parameter description:
1. Tables: Table Name, View
2. primarykey: Primary Key
3. Sort: Sorting statement without order by such as newsid DESC and orderrows ASC
4. currentpage: Current page number
5. pagesize: page size
6. filter: Filter statement without where
7. Group: Group statement without group
Effect Demo: http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
**************************************** ***********************/
(
@ Tables varchar (2000 ),
Primarykey varchar (500 ),
@ Sort varchar (500) = NULL,
@ Currentpage Int = 1,
@ Pagesize Int = 10,
@ Fields varchar (2000) = '*',
@ Filter varchar (1000) = NULL,
@ Group varchar (1000) = NULL
)
As
/** // * Default sorting */
If @ sort is null or @ sort =''
Set @ sort = @ primarykey
Declare @ sorttable varchar (1000)
Declare @ sortname varchar (1000)
Declare @ strsortcolumn varchar (1000)
Declare @ operator char (2)
Declare @ Type varchar (1000)
Declare @ prec int
/** // * Set the sorting statement .*/
If charindex ('desc', @ sort)> 0
Begin
Set @ strsortcolumn = Replace (@ sort, 'desc ','')
Set @ operator = '<='
End
Else
Begin
If charindex ('asc ', @ sort) = 0
Set @ strsortcolumn = Replace (@ sort, 'asc ','')
Set @ operator = '> ='
End
If charindex ('.', @ strsortcolumn)> 0
Begin
Set @ sorttable = substring (@ strsortcolumn, 0, charindex ('.', @ strsortcolumn ))
Set @ sortname = substring (@ strsortcolumn, charindex ('.', @ strsortcolumn) + 1, Len (@ strsortcolumn ))
End
Else
Begin
Set @ sorttable = @ tables
Set @ sortname = @ strsortcolumn
End
Select @ type = T. Name, @ prec = C. prec
From sysobjects o
Join syscolumns C on O. ID = C. ID
Join policypes t on C. xusertype = T. xusertype
Where o. Name = @ sorttable and C. Name = @ sortname
If charindex ('Char ', @ type)> 0
Set @ type = @ Type + '(' + Cast (@ prec as varchar) + ')'
Declare @ strpagesize varchar (500)
Declare @ strstartrow varchar (500)
Declare @ strfilter varchar (1000)
Declare @ strsimplefilter varchar (1000)
Declare @ strgroup varchar (1000)
/** // * Default current page */
If @ currentpage <1
Set @ currentpage = 1
/** // * Set the paging parameter .*/
Set @ strpagesize = cast (@ pagesize as varchar (500 ))
Set @ strstartrow = cast (@ currentpage-1) * @ pagesize + 1) as varchar (500 ))
/** // * Filter and group statement .*/
If @ filter is not null and @ filter! =''
Begin
Set @ strfilter = 'where' + @ filter +''
Set @ strsimplefilter = 'and' + @ filter +''
End
Else
Begin
Set @ strsimplefilter =''
Set @ strfilter =''
End
If @ group is not null and @ group! =''
Set @ strgroup = 'group by' + @ group +''
Else
Set @ strgroup =''
/** // * Execute the query statement */
Exec (
'
Declare @ sortcolumn '+ @ Type +'
Set rowcount '+ @ strstartrow +'
Select @ sortcolumn = '+ @ strsortcolumn + 'from' + @ tables + @ strfilter + ''+ @ strgroup + 'ORDER BY' + @ sort +'
Set rowcount '+ @ strpagesize +'
Select '+ @ fields + 'from' + @ tables + 'where' + @ strsortcolumn + @ operator +' @ sortcolumn '+ @ strsimplefilter + ''+ @ strgroup + 'order '+ @ sort +'
'
)


This method is used to store data, obtain data, bind data to a data control, and provide a pagedata class.

/** // <Summary>
/// Provide the data source
/// </Summary>
Public class pagedata
{
Private int _ pagesize = 10;
Private int _ pageindex = 1;
Private int _ pagecount = 0;
Private int _ totalcount = 0;
Private string _ tablename; // table name
Private string _ queryfieldname = "*"; // table field fieldstr
Private string _ orderstr = string. Empty; // sort _ sortstr
Private string _ querycondition = string. Empty; // query condition rowfilter
Private string _ primarykey = string. Empty; // primary key
/** // <Summary>
/// Display page number
/// </Summary>
Public int pagesize
{
Get
{
Return _ pagesize;

}
Set
{
_ Pagesize = value;
}
}
/** // <Summary>
/// Current page
/// </Summary>
Public int pageindex
{
Get
{
Return _ pageindex;
}
Set
{
_ Pageindex = value;
}
}
/** // <Summary>
/// Total number of pages
/// </Summary>
Public int pagecount
{
Get
{
Return _ pagecount;
}
}
/** // <Summary>
/// Total number of records
/// </Summary>
Public int totalcount
{
Get
{
Return _ totalcount;
}
}
/** // <Summary>
/// Table name, including View
/// </Summary>
Public String tablename
{
Get
{
Return _ tablename;
}
Set
{
_ Tablename = value;
}
}
/** // <Summary>
/// Table field fieldstr
/// </Summary>
Public String queryfieldname
{
Get
{
Return _ queryfieldname;
}
Set
{
_ Queryfieldname = value;
}
}
/** // <Summary>
/// Sorting Field
/// </Summary>
Public String orderstr
{
Get
{
Return _ orderstr;
}
Set
{
_ Orderstr = value;
}
}
/** // <Summary>
/// Query Conditions
/// </Summary>
Public String querycondition
{
Get
{
Return _ querycondition;
}
Set
{
_ Querycondition = value;
}
}
/** // <Summary>
/// Primary key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.