C# WinForm分頁控件實現與使用詳解
概述
在WinForms應用程序開發中,數據分頁是常見的需求。本文將介紹如何實現一個功能完整的分頁控件,并在窗體中如何使用該控件進行數據分頁展示。
分頁控件實現
核心屬性與字段
public partial class PageControl : UserControl {public int currentPage = 1; // 當前頁碼public int pageSize = 20; // 每頁數據量public int totalCount = 0; // 總數據條數public int TotalPages = 0; // 總頁數private BindingSource bindingSource = new BindingSource();public delegate void ShowDataDelegate(); // 數據展示委托public ShowDataDelegate showDataDelegate; // 委托實例public bool isOK = false; // 控件就緒標志 }
控件初始化
public PageControl() {this.InitializeComponent();// 初始化每頁顯示數量選項this.comboBoxPageSize.Items.AddRange(new object[5]{(object) 20,(object) 50,(object) 80,(object) 100,(object) 150});this.comboBoxPageSize.SelectedIndex = 0; }
頁面導航功能
private void NavigateTo(int page) {if (isOK){if (page < 1 || page > this.TotalPages)return;this.currentPage = page;} }// 頁面跳轉按鈕事件處理 private void btnFirst_Click(object sender, EventArgs e) { this.NavigateTo(1); showDataDelegate(); }private void btnPrevious_Click(object sender, EventArgs e) {this.NavigateTo(this.currentPage - 1);showDataDelegate(); }private void btnNext_Click(object sender, EventArgs e) {this.NavigateTo(this.currentPage + 1); showDataDelegate(); }private void btnLast_Click(object sender, EventArgs e) { this.NavigateTo(this.TotalPages); showDataDelegate(); }
頁碼輸入處理
private void BtnGo_Click(object sender, EventArgs e) {int result;if (int.TryParse(this.txtGoPage.Text, out result)){this.NavigateTo(result);showDataDelegate();}else{MessageBox.Show("非法字符!");} }// 限制只能輸入數字和回退鍵 private void txtGoPage_KeyPress(object sender, KeyPressEventArgs e) {if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b')e.Handled = true;if (e.KeyChar != '\r')return;this.BtnGo_Click(sender, (EventArgs)e); }
頁面信息更新
public void UpdatePageInfo() {this.TotalPages = this.totalCount % this.PageSize != 0 ? this.totalCount / this.PageSize + 1 : this.totalCount / this.PageSize;this.lblPageInfo.Text = string.Format("第 {0} 頁 / 共 {1} 頁(共 {2} 條數據)", this.currentPage, this.TotalPages, this.totalCount); }
在窗體中使用分頁控件
public FrmQRcodeRecordMG() {InitializeComponent();DgvData.AutoGenerateColumns = false;ShowDgvSetting();// 初始化下拉框CmbFaild.DisplayMember = "Value";CmbFaild.ValueMember = "Key";CmbFaild.DataSource = dicHeader.ToList();CmbFaild.SelectedIndex = 0;// 綁定分頁控件事件pageControl1.showDataDelegate += new PageControl.ShowDataDelegate(PageQuery);pageControl1.isOK = true; }
分頁查詢實現
private void PageQuery() {if (this.InvokeRequired){this.Invoke(new System.Action(() => { PageQuery(); }));return;}try{ConditionalType conditionalType = ConditionalType.Like;Dictionary<string, object> dic = new Dictionary<string, object>();// 調用服務層獲取分頁數據allData = QRCodeService.GetPageDataByDic(pageControl1.currentPage, pageControl1.pageSize, ref pageControl1.totalCount, dic, conditionalType);// 更新分頁信息pageControl1.UpdatePageInfo();// 更新DataGridView數據源if (DgvData.DataSource != null){this.BindingContext[DgvData.DataSource].SuspendBinding();}DgvData.DataSource = new List<object>();DgvData.DataSource = allData;this.BindingContext[DgvData.DataSource].ResumeBinding();}catch (Exception ex){CommonFunc.ShowTip(ex.Message);} }
使用說明
控件初始化:將PageControl添加到窗體后,需要設置
showDataDelegate
委托并設置isOK = true
數據查詢:實現分頁查詢方法,并在其中調用服務層獲取數據
頁面更新:獲取數據后調用
UpdatePageInfo()
方法更新分頁信息數據綁定:將獲取的數據綁定到DataGridView或其他數據顯示控件
總結
本文介紹了一個功能完整的WinForms分頁控件的實現與使用方法。該控件提供了基本的頁面導航功能,包括首頁、上一頁、下一頁、末頁按鈕,頁碼輸入框以及每頁顯示數量的選擇。通過委托機制,實現了控件與具體數據查詢邏輯的解耦,使控件具有更好的復用性。
在實際使用中,只需將控件添加到窗體,實現數據查詢方法,并將方法綁定到控件的委托上即可輕松實現數據分頁功能。這種設計模式使得分頁邏輯與業務邏輯分離,提高了代碼的可維護性和可擴展性。
希望本文對你在WinForms開發中實現分頁功能有所幫助!