devexprsss自带的分页控件功能单一,且用户体验感差,为此,本人自定义了一个分页控件。废话不多说,上效果图!
下面是分页控件设计窗口:
下面是分页控件代码:
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 IMS_TOOLS.Unit.Unit; namespace IMS_TOOLS.UI.pageControl { public partial class pageControl : UserControl { public delegate void GetPageDataEvents();//定义委托 public event GetPageDataEvents getDataEvnet;//定义事件 public pageControl() { InitializeComponent(); } /// <summary> /// 总记录数 /// </summary> private int record = 0; public int RecordNum { get { return record; } set { record = value; } } /// <summary> /// 每页条数 /// </summary> private int pageSize = 10; public int PageSize { get { return pageSize; } set { pageSize = value; } } /// <summary> /// 当前页 /// </summary> private int currentPage = 1; public int CurrentPage { get { return currentPage; } set { currentPage = value; } } /// <summary> /// 总页数 /// </summary> public int pageNum = 1; public int PageNum { get { if (RecordNum == 0) { pageNum = 1; } else { if (RecordNum % PageSize > 0) { pageNum = RecordNum / PageSize + 1; } else { pageNum = RecordNum / PageSize; } } return pageNum; } } public void SetSpinHide() { layoutControlItem8.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; layoutControlItem9.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; } /// <summary> /// 改变分页控件的显示 /// </summary> public void InitPageInfo() { this.labelInfo.Text = string.Format("页/共{0}页({1}笔记录)", PageNum, RecordNum); this.txtNumber.Text = CurrentPage.ToString(); this.spinPageSize.Text = PageSize.ToString(); if (CurrentPage == 0 || CurrentPage > pageNum) { MessageDxUtil.ShowTips("输入的页码不存在!"); } // 设置按钮状态 if (1 == CurrentPage) { btnFirstPage.Enabled = false; btnPrePage.Enabled = false; } if (1 == PageNum) { btnFirstPage.Enabled = false; btnPrePage.Enabled = false; btnLastPage.Enabled = false; btnNextPage.Enabled = false; } if (CurrentPage == PageNum) { btnLastPage.Enabled = false; btnNextPage.Enabled = false; } } /// <summary> /// 按钮点击事件 /// </summary> private void BtnEvents(object sender, EventArgs e) { if (getDataEvnet != null) { btnPrePage.Enabled = true; btnNextPage.Enabled = true; btnLastPage.Enabled = true; btnFirstPage.Enabled = true; DevExpress.XtraEditors.SimpleButton button = sender as DevExpress.XtraEditors.SimpleButton; string btmTag = button.Tag + ""; if (btmTag == "first")//首页按钮单击事件出发 { CurrentPage = 1; getDataEvnet(); btnPrePage.Enabled = false; btnFirstPage.Enabled = false; } if (btmTag == "prv")//上一页按钮单击事件被触发 { CurrentPage -= 1; getDataEvnet(); this.txtNumber.Text = CurrentPage.ToString(); if (CurrentPage == 1) { btnPrePage.Enabled = false; btnFirstPage.Enabled = false; } } if (btmTag == "next") { CurrentPage += 1; getDataEvnet(); this.txtNumber.Text = CurrentPage.ToString(); if (CurrentPage == PageNum) { btnNextPage.Enabled = false; btnLastPage.Enabled = false; } } if (btmTag == "last") { CurrentPage = PageNum; getDataEvnet(); btnNextPage.Enabled = false; btnLastPage.Enabled = false; } } } /// <summary> /// 每页大小改变事件 /// </summary> private void spinPageSize_EditValueChanged(object sender, EventArgs e) { if (getDataEvnet != null) { btnPrePage.Enabled = true; btnNextPage.Enabled = true; btnLastPage.Enabled = true; btnFirstPage.Enabled = true; PageSize = Convert.ToInt32(spinPageSize.Text); getDataEvnet(); } } } }
下面是控件的调用:
在要使用分页控件的类中定义分页控件的实例pageCtrl,然后在构造函数中关注分页控件的事件 ,如下代码:
public InspectResultRoTaskInfo() { InitializeComponent(); pageCtrl.getDataEvnet += getDataFromDB; }紧接着是实现分页控件事件的详细处理(查询数据库,并更新分页控件信息),代码如下:
private void getDataFromDB() { //显示等待窗口 SplashScreenManager.ShowForm(typeof(WaitForm1)); int recordCount = 0; DataTable table = inspectResultRoTaskService.getResultRoTaskInfo(pageCtrl.CurrentPage, pageCtrl.PageSize, out recordCount); table.Columns.Add("CHECKED", System.Type.GetType("System.Boolean")); foreach (DataRow row in table.Rows) { if (0 == string.Compare("3", row["TASK_STATUS"] + "")) { row["CHECKED"] = true; BaseUtil.TASK_ID = row["TASK_ID"] + ""; } } gridCtrlTask.DataSource = table; pageCtrl.RecordNum = recordCount; pageCtrl.InitPageInfo(); //关闭等待窗口 SplashScreenManager.CloseForm(); }
