一、使用到的WinForm控件有连个toolStrip和一个imageList, imageList主要负责保存四个翻页的小图片,
二、分页控件工作原理
1.首先让控件继承UserControl, INotifyPropertyChanged,继承INotifyPropertyChanged,主要是实现当用户点击翻页或者更改文本框里的数字,那么当前列表自动更新信息,如果要做成点击按钮翻页可以直接写个委托来定义一个事件就可以了,这里这么实现也是为了操作方便。
实现代码:
public
partial
class
WinFromPager : UserControl, INotifyPropertyChanged
{
}
2. 定义事件模型,实际上时定义一个在列表页面执行翻页的事件
{
}
public
event
PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged( string propertyName)
{
if (PropertyChanged != null )
{
PropertyChanged( this , new PropertyChangedEventArgs(propertyName));
}
}
3. 定义属性
protected virtual void OnPropertyChanged( string propertyName)
{
if (PropertyChanged != null )
{
PropertyChanged( this , new PropertyChangedEventArgs(propertyName));
}
}
private
int
_mixPage
=
1
;
private int _maxPage = 1 ;
private int _currentPage = 1 ;
private int _RecordCount = 0 ;
private int _pageSize = 20 ;
private string _lblPageInfo = " 当前页({0})/总共({1})页,共({2})条记录,每页({3})条 " ;
private int _maxPage = 1 ;
private int _currentPage = 1 ;
private int _RecordCount = 0 ;
private int _pageSize = 20 ;
private string _lblPageInfo = " 当前页({0})/总共({1})页,共({2})条记录,每页({3})条 " ;
///
<sumary>
/// 第一页索引值
/// </summary>
public int MixPage
{
get
{
return _mixPage;
}
set
{
_mixPage = value;
}
}
/// <summary>
/// 总页数
/// </summary>
public int MaxPage
{
get
{
return _maxPage;
}
set
{
_maxPage = value;
}
}
/// <summary>
/// 当前页索引
/// </summary>
public int CurrentPage
{
get
{
return this ._currentPage;
}
set
{
this ._currentPage = value;
}
}
/// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get
{
return this ._pageSize;
}
set
{
this ._pageSize = value;
}
}
/// <summary>
/// 开始索引
/// </summary>
public int StartIndex
{
get
{
return ((CurrentPage - 1 ) * PageSize) + 1 ;
}
}
/// <summary>
/// 结束索引
/// </summary>
public int EndIndex
{
get
{
if ((RecordCount - ((CurrentPage - 1 ) * PageSize)) >= PageSize)
{
return CurrentPage * PageSize ;
}
else
{
return RecordCount;
}
}
}
/// <summary>
/// 总记录
/// </summary>
public int RecordCount
{
set
{
_RecordCount = value;
GetPageCount();
}
get
{
return _RecordCount;
}
}
/// <summary>
/// 查询当前索引页
/// </summary>
public int CurrentIndex
{
set
{
this ._currentPage = value;
this .lblCurrectPage.Text = _mixPage.ToString();
}
}
4. 显示页面分页信息
/// 第一页索引值
/// </summary>
public int MixPage
{
get
{
return _mixPage;
}
set
{
_mixPage = value;
}
}
/// <summary>
/// 总页数
/// </summary>
public int MaxPage
{
get
{
return _maxPage;
}
set
{
_maxPage = value;
}
}
/// <summary>
/// 当前页索引
/// </summary>
public int CurrentPage
{
get
{
return this ._currentPage;
}
set
{
this ._currentPage = value;
}
}
/// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get
{
return this ._pageSize;
}
set
{
this ._pageSize = value;
}
}
/// <summary>
/// 开始索引
/// </summary>
public int StartIndex
{
get
{
return ((CurrentPage - 1 ) * PageSize) + 1 ;
}
}
/// <summary>
/// 结束索引
/// </summary>
public int EndIndex
{
get
{
if ((RecordCount - ((CurrentPage - 1 ) * PageSize)) >= PageSize)
{
return CurrentPage * PageSize ;
}
else
{
return RecordCount;
}
}
}
/// <summary>
/// 总记录
/// </summary>
public int RecordCount
{
set
{
_RecordCount = value;
GetPageCount();
}
get
{
return _RecordCount;
}
}
/// <summary>
/// 查询当前索引页
/// </summary>
public int CurrentIndex
{
set
{
this ._currentPage = value;
this .lblCurrectPage.Text = _mixPage.ToString();
}
}
///
<summary>
/// 显示页面分页信息
/// </summary>
private void GetPageCount()
{
if (RecordCount == 0 )
{
MaxPage = 1 ;
}
else
{
if ( this .RecordCount % PageSize == 0 )
{
MaxPage = this .RecordCount / PageSize;
}
else
{
MaxPage = this .RecordCount / PageSize + 1 ;
}
}
if ( this .CurrentPage == MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
if (RecordCount <= PageSize)
{
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else
{
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
}
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
}
5. 定义事件
/// 显示页面分页信息
/// </summary>
private void GetPageCount()
{
if (RecordCount == 0 )
{
MaxPage = 1 ;
}
else
{
if ( this .RecordCount % PageSize == 0 )
{
MaxPage = this .RecordCount / PageSize;
}
else
{
MaxPage = this .RecordCount / PageSize + 1 ;
}
}
if ( this .CurrentPage == MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
if (RecordCount <= PageSize)
{
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else
{
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
}
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
}
//
首页
private void First_Click( object sender, EventArgs e)
{
this .CurrentPage = MixPage;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
OnPropertyChanged( " CurrentPage " );
}
// 上一页
private void Pre_Click( object sender, EventArgs e)
{
this .CurrentPage = this .CurrentPage - 1 ;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == this .MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
OnPropertyChanged( " CurrentPage " );
}
// 下一页
private void Next_Click( object sender, EventArgs e)
{
this .CurrentPage = this .CurrentPage + 1 ;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == MaxPage)
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
OnPropertyChanged( " CurrentPage " );
}
// 末页
private void Last_Click( object sender, EventArgs e)
{
this .CurrentPage = MaxPage;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
OnPropertyChanged( " CurrentPage " );
}
private void lblCurrectPage_TextChanged( object sender, EventArgs e)
{
if ( ! String.IsNullOrEmpty(lblCurrectPage.Text))
{
this .CurrentPage = int .Parse(lblCurrectPage.Text);
}
else
{
this .CurrentPage = MixPage;
}
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == this .MaxPage)
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else if ( this .CurrentPage == this .MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
if ( this .lblCurrectPage.Text != "" )
{
if ( int .Parse( this .lblCurrectPage.Text) > MaxPage)
{
MessageBox.Show( " 不存在输入页码数据! " , " 输入信息错误 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
this .lblCurrectPage.Text = MixPage.ToString();
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
}
OnPropertyChanged( " CurrentPage " );
}
private void lblCurrectPage_KeyPress( object sender, KeyPressEventArgs e)
{
if ((e.KeyChar != 8 && ! char .IsDigit(e.KeyChar)) && e.KeyChar != 13 )//文本框里只能输入数字
{
e.Handled = true ;
}
}
控件基本完成。
private void First_Click( object sender, EventArgs e)
{
this .CurrentPage = MixPage;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
OnPropertyChanged( " CurrentPage " );
}
// 上一页
private void Pre_Click( object sender, EventArgs e)
{
this .CurrentPage = this .CurrentPage - 1 ;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == this .MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
OnPropertyChanged( " CurrentPage " );
}
// 下一页
private void Next_Click( object sender, EventArgs e)
{
this .CurrentPage = this .CurrentPage + 1 ;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == MaxPage)
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
OnPropertyChanged( " CurrentPage " );
}
// 末页
private void Last_Click( object sender, EventArgs e)
{
this .CurrentPage = MaxPage;
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
OnPropertyChanged( " CurrentPage " );
}
private void lblCurrectPage_TextChanged( object sender, EventArgs e)
{
if ( ! String.IsNullOrEmpty(lblCurrectPage.Text))
{
this .CurrentPage = int .Parse(lblCurrectPage.Text);
}
else
{
this .CurrentPage = MixPage;
}
this .lblPageInfo.Text = string .Format(_lblPageInfo, this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
this .lblMaxPage.Text = " / " + MaxPage;
this .lblCurrectPage.Text = CurrentPage.ToString();
if ( this .CurrentPage == this .MaxPage)
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
else if ( this .CurrentPage == this .MixPage)
{
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
else
{
this .First.Enabled = true ;
this .Pre.Enabled = true ;
this .Next.Enabled = true ;
this .Last.Enabled = true ;
}
if ( this .lblCurrectPage.Text != "" )
{
if ( int .Parse( this .lblCurrectPage.Text) > MaxPage)
{
MessageBox.Show( " 不存在输入页码数据! " , " 输入信息错误 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
this .lblCurrectPage.Text = MixPage.ToString();
this .First.Enabled = false ;
this .Pre.Enabled = false ;
this .Next.Enabled = false ;
this .Last.Enabled = false ;
}
}
OnPropertyChanged( " CurrentPage " );
}
private void lblCurrectPage_KeyPress( object sender, KeyPressEventArgs e)
{
if ((e.KeyChar != 8 && ! char .IsDigit(e.KeyChar)) && e.KeyChar != 13 )//文本框里只能输入数字
{
e.Handled = true ;
}
}
三、调用方法
在列表页面的调用方法:
绑定列表信息:
private
void
Bind()
{
DataList.DataBoxList(dataGridView1, winFromPager1.StartIndex, winFromPager1.EndIndex, strWhere.ToString());
}
dataGridView1:绑定列表控件名称
{
DataList.DataBoxList(dataGridView1, winFromPager1.StartIndex, winFromPager1.EndIndex, strWhere.ToString());
}
winFromPager1.StartIndex:开始索引
winFromPager1.EndIndex: 结束索引
strWhere.ToString(): 查询条件
分页事件:
private
void
winFromPager1_PropertyChanged(
object
sender, PropertyChangedEventArgs e)
{
Bind(); //绑定列表
}
效果图:
{
Bind(); //绑定列表
}