C# Winform 分頁(yè)功能的實(shí)現(xiàn)
首先創(chuàng)建一個(gè)用戶控件 如下圖 用到的控件 label、button、TextBox

內(nèi)部代碼如下
#region 分頁(yè)字段和屬性
private int pageIndex = 1;
/// <summary>
/// 當(dāng)前頁(yè)數(shù)
/// </summary>
public virtual int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
private int pageSize = 100;
/// <summary>
/// 每頁(yè)記錄數(shù)
/// </summary>
public virtual int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
private int recordCount = 0;
/// <summary>
/// 總記錄數(shù)
/// </summary>
public virtual int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
private int pageCount = 0;
/// <summary>
/// 總頁(yè)數(shù)
/// </summary>
public int PageCount
{
get
{
if (pageSize != 0)
{
pageCount = GetPageCount();
}
return pageCount;
}
}
#endregion
#region 頁(yè)碼變化時(shí)觸發(fā)事件
public event EventHandler OnPageChanged;
#endregion
#region 分頁(yè)及相關(guān)事件功能實(shí)現(xiàn)
/// <summary>
/// 設(shè)窗體控件全部可用
/// </summary>
private void SetFormCtrEnabled()
{
linkFirst.Enabled = true;
linkPrevious.Enabled = true;
linkNext.Enabled = true;
linkLast.Enabled = true;
btnGo.Enabled = true;
}
/// <summary>
/// 計(jì)算總頁(yè)數(shù)
/// </summary>
/// <returns></returns>
private int GetPageCount()
{
if (PageSize == 0)
{
return 0;
}
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize == 0)
{
pageCount = RecordCount / PageSize;
}
else
{
pageCount = RecordCount / PageSize + 1;
}
return pageCount;
}
/// <summary>
/// 用于客戶端調(diào)用
/// </summary>
public void DrawControl(int count)
{
recordCount = count;
DrawControl(false);
}
/// <summary>
/// 根據(jù)不同的條件,改變頁(yè)面控件的呈現(xiàn)狀態(tài)
/// </summary>
private void DrawControl(bool callEvent)
{
lblCurrentPage.Text = PageIndex.ToString();
lblPageCount.Text = PageCount.ToString();
lblTotalCount.Text = RecordCount.ToString();
txtPageSize.Text = PageSize.ToString();
if (callEvent && OnPageChanged != null)
{
OnPageChanged(this, null);//當(dāng)前分頁(yè)數(shù)字改變時(shí),觸發(fā)委托事件
}
SetFormCtrEnabled();
if (PageCount == 1)//有且僅有一頁(yè)時(shí)
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
linkNext.Enabled = false;
linkLast.Enabled = false;
btnGo.Enabled = false;
}
else if (PageIndex == 1)//當(dāng)前頁(yè)為第一頁(yè)時(shí)
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
}
else if (PageIndex == PageCount)//當(dāng)前頁(yè)為最后一頁(yè)時(shí)
{
linkNext.Enabled = false;
linkLast.Enabled = false;
}
}
#endregion
#region 相關(guān)控件事件
//首頁(yè)按鈕
private void linkFirst_Click(object sender, EventArgs e)
{
PageIndex = 1;
DrawControl(true);
}
//上一頁(yè)按鈕
private void linkPrevious_Click(object sender, EventArgs e)
{
PageIndex = Math.Max(1, PageIndex - 1);
DrawControl(true);
}
//下一頁(yè)按鈕
private void linkNext_Click(object sender, EventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + 1);
DrawControl(true);
}
//尾頁(yè)按鈕
private void linkLast_Click(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
}
/// <summary>
/// 按下enter鍵,執(zhí)行跳轉(zhuǎn)頁(yè)面功能
/// </summary>
private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
{
btnGo_Click(null, null);
}
/// <summary>
/// 跳轉(zhuǎn)頁(yè)數(shù)限制
/// </summary>
private void txtPageNum_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{ //TryParse 函數(shù),將字符串轉(zhuǎn)換成等效的整數(shù),返回bool型,判斷是否轉(zhuǎn)換成功。
//輸入除數(shù)字以外的字符是轉(zhuǎn)換不成功的
if (num > PageCount) //輸入數(shù)量大于最大頁(yè)數(shù)時(shí),文本框自動(dòng)顯示最大頁(yè)數(shù)
{
txtPageNum.Text = PageCount.ToString();
}
}
}
/// <summary>
/// 跳轉(zhuǎn)按鈕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGo_Click(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{
PageIndex = num;
DrawControl(true);
}
}
#endregion
bool isTextChanged = false;
/// <summary>
/// 每頁(yè)顯示的記錄數(shù)改變時(shí)
/// </summary>
private void txtPageSize_TextChanged(object sender, EventArgs e)
{
int num = 0;
//輸入不符合規(guī)范時(shí),默認(rèn)設(shè)置為100
if (!int.TryParse(txtPageSize.Text.Trim(), out num) || num <= 0)
{
num = 100;
txtPageSize.Text = "100";
}
else
{
isTextChanged = true;
}
pageSize = num;
}
/// <summary>
/// 光標(biāo)離開(kāi) 每頁(yè)設(shè)置文本框時(shí),顯示到首頁(yè)
private void txtPageSize_Leave(object sender, EventArgs e)
{
if (isTextChanged)
{
isTextChanged = false;
linkFirst_Click(null, null);
}
}符合我的分頁(yè)查詢方法
/// <summary>
/// 通用分頁(yè)查詢方法
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="strColumns">查詢字段名</param>
/// <param name="strWhere">where條件</param>
/// <param name="strOrder">排序條件</param>
/// <param name="pageSize">每頁(yè)數(shù)據(jù)數(shù)量</param>
/// <param name="currentIndex">當(dāng)前頁(yè)數(shù)</param>
/// <param name="recordOut">數(shù)據(jù)總量</param>
/// <returns>DataTable數(shù)據(jù)表</returns>
public static List<String[]> SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
{
List<String[]> res;
recordOut = Convert.ToInt32(search(string.Format("select count(*) from {0} where{1}" ,tableName, strWhere))[0][0]);
string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
int offsetCount = (currentIndex - 1) * pageSize;
string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString());
res = search(commandText);
return res;
}附上我修改前的分頁(yè)查詢方法
/// <summary>
/// 通用分頁(yè)查詢方法
/// </summary>
/// <param name="connString">連接字符串</param>
/// <param name="tableName">表名</param>
/// <param name="strColumns">查詢字段名</param>
/// <param name="strWhere">where條件</param>
/// <param name="strOrder">排序條件</param>
/// <param name="pageSize">每頁(yè)數(shù)據(jù)數(shù)量</param>
/// <param name="currentIndex">當(dāng)前頁(yè)數(shù)</param>
/// <param name="recordOut">數(shù)據(jù)總量</param>
/// <returns>DataTable數(shù)據(jù)表</returns>
public static DataTable SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
{
DataTable dt = new DataTable();
recordOut = Convert.ToInt32(ExecuteScalar( "select count(*) from " + tableName));
string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
int offsetCount = (currentIndex - 1) * pageSize;
string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString());
DataSet ds= ExecuteDataSet(commandText);
dt = ds.Tables[0];
return dt;
}兩種使用方式
1、數(shù)據(jù)庫(kù)查詢
private void pagerControl1_OnPageChanged(object sender, EventArgs e)
{
initData();
}
public CompanyData()
{
InitializeComponent();
initData();
//激活OnPageChanged事件
pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
}
private void initData()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(" 1=1 ");
if (this.TbComName.Text != "")
{
stringBuilder.Append("and comName like '%" + this.TbComName.Text + "%'");
}
int RecordCount;
List<String[]> res = DBHelper.SelectPaging("T_DM_COMPANY", " comId,comCode,comName,configureCode", stringBuilder.ToString(), " configureCode DESC ,comCode", pagerControl1.PageSize, pagerControl1.PageIndex, out RecordCount);
List<Company> list = new List<Company>();
for (int i = 0; i < res.Count; i++)
{
list.Add(new Company(res[i]));
}
pagerControl1.DrawControl(RecordCount);
BindingSource bs = new BindingSource();
bs.DataSource = list;
this.CompanyDataDGW.DataSource = bs;
}2、文件讀取數(shù)據(jù) 分頁(yè)
private void pagerControl1_OnPageChanged(object sender, EventArgs e)
{
Init();
}
public ProductionConfig()
{
InitializeComponent();
Init();
//激活OnPageChanged事件
pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
}
private void Init()
{
//獲取template 中ip配置文件信息
//String[] temIp = PropertiesGenerateUtil.getTemplate("template/ip.properties");
int RecordCount;
String[] temIp = PropertiesGenerateUtil.getTemplatePaging("template/ip.properties",pagerControl1.PageSize,pagerControl1.PageIndex,out RecordCount);
List<entity.templateIp> templateIps = new List<entity.templateIp>();
foreach (var item in temIp)
{
templateIps.Add(new entity.templateIp(item.Substring(0, item.IndexOf("="))));
}
pagerControl1.DrawControl(RecordCount);
BindingSource bs = new BindingSource();
bs.DataSource = templateIps;
this.DGVTemplateIp.DataSource = bs;
}
/// <summary>
/// 分頁(yè)查詢文件數(shù)據(jù)
/// </summary>
/// <param name="path">文件路徑</param>
/// <param name="pageSize">每頁(yè)數(shù)量</param>
/// <param name="currentIndex">當(dāng)前頁(yè)數(shù)</param>
/// <param name="recordOut">數(shù)據(jù)總量</param>
/// <returns></returns>
public static string[] getTemplatePaging(string path, int pageSize, int currentIndex, out int recordOut)
{
string[] str;
recordOut = getTemplate(path).Count();
using (StreamReader sr = new StreamReader(path))
{
string line;
List<string> list = new List<string> { };
// 1 0 1*100
// 2 100 2*100
int startingValue = (currentIndex - 1) * pageSize;//起始值
int terminationValue = currentIndex * pageSize;//終止值
int tempValue = 0;//計(jì)數(shù)器
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("#"))//判斷是注釋跳出
continue;
if(tempValue>=startingValue&&tempValue< terminationValue)//判斷是起始值添加
list.Add(line);
if (tempValue == terminationValue)//判斷是終止值跳出
break;
tempValue++;
}
str = list.ToArray<String>();
}
return str;
}附上運(yùn)行結(jié)果

到此這篇關(guān)于C# Winform 分頁(yè)功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# Winform 分頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Excel動(dòng)態(tài)函數(shù)實(shí)現(xiàn)生成依賴列表
這篇文章主要為大家詳細(xì)介紹了如何在C#中使用?Excel?動(dòng)態(tài)函數(shù)生成依賴列表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
C#函數(shù)式編程中的標(biāo)準(zhǔn)高階函數(shù)詳解
這篇文章主要介紹了C#函數(shù)式編程中的標(biāo)準(zhǔn)高階函數(shù)詳解,本文講解了何為高階函數(shù)、Map、 Filter、Fold等內(nèi)容,需要的朋友可以參考下2015-01-01
C#實(shí)現(xiàn)3步手動(dòng)建DataGridView的方法
這篇文章主要介紹了C#實(shí)現(xiàn)3步手動(dòng)建DataGridView的方法,實(shí)例分析了C#實(shí)現(xiàn)手動(dòng)創(chuàng)建DataGridView的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#/VB.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF
可擴(kuò)展標(biāo)記語(yǔ)言(XML)文件是一種標(biāo)準(zhǔn)的文本文件,它使用特定的標(biāo)記來(lái)描述文檔的結(jié)構(gòu)以及其他特性。本文將利用C#實(shí)現(xiàn)XML文件轉(zhuǎn)PDF?,需要的可以參考一下2022-03-03

