实现效果 有点丑陋 但是功能是没问题的 测试过
实现思路
先创建一个用户控件
代码实现
public partial class PagerControl : UserControl { private int record = 0; /// <summary> /// 总记录数 /// </summary> public int Record { get { return record; } set { record = value; InitPageInfo(); } } private int pageSize = 20; /// <summary> /// 每页条数 /// </summary> public int PageSize { get { return pageSize; } set { pageSize = value; } } private int currentPage = 1; /// <summary> /// 当前页 /// </summary> public int CurrentPage { get { return currentPage; } set { currentPage = value; } } public int pageNum = 0; /// <summary> /// 总页码 /// </summary> public int PageNum { get { if (Record == 0) { pageNum = 0; } else { if (Record % PageSize > 0) { pageNum = Record / PageSize + 1; } else { pageNum = Record / PageSize; } } return pageNum; } } //定义委托 public delegate void BindHandle(object sender, EventArgs e); /// <summary> /// 绑定数据源事件 /// </summary> public event BindHandle BindSource; public PagerControl() { InitializeComponent(); } /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFirst_Click(object sender, EventArgs e) { if (Record > 0) { if (CurrentPage == 1) { MessageBox.Show("当前已经是首页"); return; } else { CurrentPage = 1; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } } private void btnPre_Click(object sender, EventArgs e) { if (Record > 0) { if (CurrentPage == 1) { MessageBox.Show("当前已经是首页"); return; } else { CurrentPage = CurrentPage - 1; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } } private void btnNext_Click(object sender, EventArgs e) { if (Record > 0) { if (CurrentPage == PageNum) { MessageBox.Show("当前已经是末页"); return; } else { CurrentPage = CurrentPage + 1; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } } private void btnLast_Click(object sender, EventArgs e) { if (Record > 0) { if (CurrentPage == PageNum) { MessageBox.Show("当前已经是末页"); return; } else { CurrentPage = PageNum; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } } private void InitPageInfo() { lblInfo.Text = string.Format("共{0}条记录 共{1}页 当前第{2}页", Record, PageNum,CurrentPage); txtPage.Text = CurrentPage.ToString(); } private void btnGo_Click(object sender, EventArgs e) { if (Record > 0) { if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$")) { MessageBox.Show("请正确填写页码!"); return; } int page = Convert.ToInt32(txtPage.Text); if (page == 0) { page = 1; } if (page > PageNum) { page = PageNum; } CurrentPage = page; if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } } private void PagerControl_Load(object sender, EventArgs e) { if (BindSource != null) { BindSource(sender, e); InitPageInfo(); } } }
使用
只要在窗体中 写好绑定方法
private void Bind() { string start = dtpDate1.Value.ToString("yyyy-MM-dd"); string end = dtpDate2.Value.ToString("yyyy-MM-dd"); string team = cbxTeam.SelectedValue.ToString(); string jieshu = cbxSFJS.SelectedValue.ToString(); int record = 0; DataTable dt = eventBiz.GetEvents(start, end, team, jieshu, pagerControl1.CurrentPage, pagerControl1.PageSize,out record); pagerControl1.Record = record; dgvEvent.AutoGenerateColumns = false; dgvEvent.DataSource = dt.DefaultView; }
捆绑绑定事件
/// <summary> /// 绑定数据源 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pagerControl1_BindSource(object sender, EventArgs e) { Bind(); }
就可以了 需要注意的事情是由于分页控件load事件里会调用bind方法,会用到一些窗体元素的值,所以窗体元素项的初始化,应该放在窗体构造函数中,不要放在窗体load事件里。
时间: 2024-10-10 11:04:47