Windows Form 分頁 具體實現(xiàn)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Collections;
namespace Common
{
public partial class WinFormPager : UserControl
{
public event EventHandler PageChanged; //事件:控件的當(dāng)前頁碼發(fā)生變更。
private int pageSize;
private int curPage;
private int pageCount;
public WinFormPager()
{
InitializeComponent();
}
private void WinFormPager_Load(object sender, EventArgs e)
{
}
/// <summary>
/// [屬性]每頁顯示記錄數(shù)。
/// </summary>
public int PageSize
{
get
{
if (pageSize <= 0)
{
pageSize = 10;
}
return pageSize;
}
set
{
pageSize = value;
}
}
/// <summary>
/// 當(dāng)前頁數(shù)
/// </summary>
public int CurPage
{
get
{
if (curPage <= 0)
{
curPage = 1;
}
return curPage;
}
set
{
curPage = value;
if (PageChanged != null)
{
SafeRaise.Raise(PageChanged,null);//觸發(fā)當(dāng)件頁碼變更事件。
}
}
}
/// <summary>
/// [屬性]總頁數(shù)。
/// </summary>
public int PageCount
{
get
{
if (RecordCount > 0)
{
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize == 0)
{
pageCount = RecordCount / PageSize;
}
else
{
pageCount = RecordCount / PageSize + 1;
}
return pageCount;
}
else
{
return 0;
}
}
set
{
pageCount = value;
}
}
/// <summary>
/// [屬性]總記錄數(shù)。
/// </summary>
public int RecordCount
{
get;
set;
}
/// <summary>
/// [屬性]相對于當(dāng)前頁的上一頁
/// </summary>
public int PrevPage
{
get
{
if (CurPage > 1)
{
return CurPage - 1;
}
return 1;
}
}
/// <summary>
/// [屬性]相對于當(dāng)前頁的下一頁
/// </summary>
public int NextPage
{
get
{
if (CurPage < PageCount)
{
return CurPage + 1;
}
return PageCount;
}
}
private void btnFirstPage_Click(object sender, EventArgs e)
{
this.CurPage = 1;
}
private void btnLastPage_Click(object sender, EventArgs e)
{
this.CurPage = this.PrevPage;
}
private void btnNextPage_Click(object sender, EventArgs e)
{
this.CurPage = this.NextPage;
}
private void btnEndPage_Click(object sender, EventArgs e)
{
this.CurPage = this.PageCount;
}
private void txtPageNumber_TextChanged(object sender, EventArgs e)
{
if (!Validator.IsNumeric(this.txtPageNumber.Text.Trim()))
{
MessageBox.Show("請輸入數(shù)字!");
}
}
private void btnJump_Click(object sender, EventArgs e)
{
if (!Validator.IsNumeric(this.txtPageNumber.Text.Trim()))//驗證輸入是否為數(shù)字
{
MessageBox.Show("請輸入數(shù)字!");
}
else
{
if (int.Parse(this.txtPageNumber.Text.Trim()) > 0)
{
if (int.Parse(this.txtPageNumber.Text.Trim()) < this.PageCount)
{
this.CurPage = int.Parse(this.txtPageNumber.Text.Trim());
}
else
{
this.CurPage = this.PageCount;
}
}
else
{
this.CurPage = 1;
}
}
}
}
}
該用戶自定義控件在頁面中取名pager
private void BindData()
{
int rowCount = 0;
pager.PageSize = 15; DataGridView.DataSource = GetList(pager.CurPage, pager.PageSize, out rowCount);
pager.RecordCount = rowCount;
pager.lbNumber.Text = string.Format("共{0}條記錄,每頁{1}條記錄,共{2}頁", pager.RecordCount.ToString(), pager.PageSize.ToString(), pager.PageCount.ToString());
}
private void Pager_PageChanged(object sender, EventArgs e)
{
BindData(); //重新對DataGridView控件的數(shù)據(jù)源進(jìn)行綁定。
}
控件
相關(guān)文章
c#重寫TabControl控件實現(xiàn)關(guān)閉按鈕的方法
這是關(guān)于c#重寫TabControl控件實現(xiàn)關(guān)閉按鈕的例子,整理了一下,與大家分享。2013-04-04C#批量插入數(shù)據(jù)到Sqlserver中的三種方式
這篇文章主要為大家詳細(xì)介紹了C#批量插入數(shù)據(jù)到Sqlserver中的三種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12WindowsForm移動一個沒有標(biāo)題欄的窗口的方法
這篇文章主要介紹了WindowsForm移動一個沒有標(biāo)題欄的窗口的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07C# WinForm應(yīng)用程序降低系統(tǒng)內(nèi)存占用方法總結(jié)
這篇文章主要介紹了C# WinForm應(yīng)用程序降低系統(tǒng)內(nèi)存占用方法總結(jié),本文總結(jié)了9個方法,同時給出了一個定期清理執(zhí)行垃圾回收代碼,需要的朋友可以參考下2014-10-10C# 使用Word模板導(dǎo)出數(shù)據(jù)的實現(xiàn)代碼
最近接到個需求,使用word模板導(dǎo)出數(shù)據(jù),怎么實現(xiàn)這個需求呢,今天小編通過實例代碼給大家介紹C# 使用Word模板導(dǎo)出數(shù)據(jù)的方法,感興趣的朋友一起看看吧2021-06-06