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ù)源進行綁定。
}
控件

相關(guān)文章
C#實現(xiàn)win10 uwp 右擊浮出窗在點擊位置
本文主要讓MenuFlyout出現(xiàn)在我們右擊位置。我們建一個ListView,然后綁定后臺,在我們ListView要右擊顯示我們的浮出,要求我們的浮出在我們點擊位置2016-10-10
c# openxml 刪除xlsx、xls的外鏈?zhǔn)纠a
要刪除一個 Excel 文件(.xlsx)中的外部鏈接(external links),你可以使用 OpenXML SDK,本文演示如何使用 OpenXML SDK 刪除外部鏈接,感興趣的朋友一起看看吧2024-01-01
C#中l(wèi)abel內(nèi)容顯示不全、不完整的解決方法
這篇文章主要介紹了C#中l(wèi)abel內(nèi)容顯示不全、不完整的解決方法,只需要把兩個屬性設(shè)置一下即可解決這個問題,需要的朋友可以參考下2015-06-06
winform 調(diào)用攝像頭掃碼識別二維碼的實現(xiàn)步驟
這篇文章主要介紹了winform 調(diào)用攝像頭掃碼識別二維碼的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用winform,感興趣的朋友可以了解下2021-02-02
C#模擬實現(xiàn)鼠標(biāo)自動點擊與消息發(fā)送功能
這篇文章主要為大家詳細介紹了C#如何利用windows api來模擬實現(xiàn)鼠標(biāo)點擊、右擊、雙擊以及發(fā)送文本功能,文中的示例代碼講解詳細,感興趣的可以了解一下2022-08-08

