/// <summary>
/// 分页操作cyt
/// </summary>
private TaskView.PagerControl UCPaging = new PagerControl();
private void InitPagingControl()
{
UCPaging.Dock = DockStyle.Fill;
this.panelControl1.Controls.Add(UCPaging);
UCPaging.DrawControl(count);
}
void UCPaging_OnPageChanged(object sender, EventArgs e)
{
LoadData();
}
void LoadData()
{
var currentPage = lstTaskView.Skip((UCPaging.PageIndex - 1) * UCPaging.PageSize).Take(UCPaging.PageSize);
this.grdTask.DataSource = currentPage;
UCPaging.DrawControl(count);
}
分页操作类//界面如图片所示
PagerControl 类:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Geoway.OMS.ProductionUI.UIs.TaskView
{
public partial class PagerControl : UserControl
{
#region 构造函数
public PagerControl()
{
InitializeComponent();
initComboBox();
}
#endregion
#region 分页字段和属性
private const string COUNT_PER_PAGE_100 = "每页100条";
private const string COUNT_PER_PAGE_10 = "每页10条";
private const string COUNT_PER_PAGE_20 = "每页20条";
private const string COUNT_PER_PAGE_50 = "每页50条";
private const string COUNT_PER_PAGE_ALL = "全部显示";
private int pageIndex = 1;
/// <summary>
/// 当前页面
/// </summary>
public virtual int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
private int pageSize = 1;
/// <summary>
/// 每页记录数
/// </summary>
public virtual int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
private int recordCount = UCTaskList.count;
/// <summary>
/// 总记录数
/// </summary>
public virtual int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
private int pageCount = 0;
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
if (pageSize != 0)
{
pageCount = GetPageCount();
}
return pageCount;
}
}
private string jumpText;
/// <summary>
/// 跳转按钮文本
/// </summary>
public string JumpText
{
get
{
if (string.IsNullOrEmpty(jumpText))
{
jumpText = "跳转";
}
return jumpText;
}
set { jumpText = value; }
}
#endregion
#region 页码变化触发事件
public event EventHandler OnPageChanged;
#endregion
#region 分页及相关事件功能实现
private void SetFormCtrEnabled()
{
lnkFirst.Enabled = true;
lnkPrev.Enabled = true;
lnkNext.Enabled = true;
lnkLast.Enabled = true;
btnGo.Enabled = true;
}
/// <summary>
/// 计算总页数
/// </summary>
/// <returns></returns>
private int GetPageCount()
{
if (PageSize == 0)
{
return 0;
}
int pageCount = RecordCount / PageSize;
if (pageCount != 0)
{
if (RecordCount % PageSize == 0)
{
pageCount = RecordCount / PageSize;
}
else
{
pageCount = RecordCount / PageSize + 1;
}
}
else
{
pageCount = 1;
}
return pageCount;
}
/// <summary>
/// 外部调用
/// </summary>
public void DrawControl(int count)
{
recordCount = count;
DrawControl(false);
}
/// <summary>
/// 页面控件呈现
/// </summary>
private void DrawControl(bool callEvent)
{
btnGo.Text = JumpText;
lblCurrentPage.Text = PageIndex.ToString();
lblPageCount.Text = PageCount.ToString();
lblTotalCount.Text = RecordCount.ToString();
//txtPageSize.Text = PageSize.ToString();
if (callEvent && OnPageChanged != null)
{
OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
}
SetFormCtrEnabled();
if (PageCount == 1)//有且仅有一页
{
lnkFirst.Enabled = false;
lnkPrev.Enabled = false;
lnkNext.Enabled = false;
lnkLast.Enabled = false;
btnGo.Enabled = false;
}
else if (PageIndex == 1)//第一页
{
lnkFirst.Enabled = false;
lnkPrev.Enabled = false;
}
else if (PageIndex == PageCount)//最后一页
{
lnkNext.Enabled = false;
lnkLast.Enabled = false;
}
}
#endregion
#region 相关控件事件
private void lnkFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageIndex = 1;
DrawControl(true);
}
private void lnkPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageIndex = Math.Max(1, PageIndex - 1);
DrawControl(true);
}
private void lnkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + 1);
DrawControl(true);
}
private void lnkLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
}
/// <summary>
/// enter键功能
/// </summary>
private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
{
btnGo_Click(null, null);
}
/// <summary>
/// 跳转页数限制
/// </summary>
private void txtPageNum_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{
if (num > PageCount)
{
txtPageNum.Text = PageCount.ToString();
}
}
}
/// <summary>
/// 跳转
/// </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>
/// textBox分页属性改变了。
/// </summary>
private void txtPageSize_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageSize.Text.Trim(), out num) && num >0)
{
//if (num > recordCount && recordCount!=0)
//{
// txtPageSize.Text = recordCount.ToString();
//}
isTextChanged = true;
}
else
{
num = 100;
txtPageSize.Text = "100";
}
//int comboxPage = GetCurrentCountPerPage();
//if (num != comboxPage)
//{
// this.comboBoxEdit1.EditValue = "每页" + num+"条";
//}
pageSize = int.Parse(txtPageSize.Text);
}
//初始化ComboBox分页控件
private void initComboBox()
{
//this.comboBoxEdit1.Properties.NullText = "请选择";
this.comboBoxEdit1.Properties.Items.Clear();
this.comboBoxEdit1.Properties.Items.Add(COUNT_PER_PAGE_10);
this.comboBoxEdit1.Properties.Items.Add(COUNT_PER_PAGE_20);
this.comboBoxEdit1.Properties.Items.Add(COUNT_PER_PAGE_50);
this.comboBoxEdit1.Properties.Items.Add(COUNT_PER_PAGE_100);
this.comboBoxEdit1.Properties.Items.Add(COUNT_PER_PAGE_ALL);
this.comboBoxEdit1.EditValue = COUNT_PER_PAGE_50;
}
bool isComboChanged = false;
//ComboBox分页值变化
private void comboBoxEdit1_SelectedValueChanged(object sender, EventArgs e)
{
pageSize = GetCurrentCountPerPage();
isComboChanged = true;
if (int.Parse(this.txtPageSize.Text) != pageSize)
{
this.txtPageSize.Text = pageSize.ToString();
}
this.txtPageNum.Text = "";
lnkFirst_LinkClicked(null, null);
}
//根据ComboBox分页控件获取每页大小
private int GetCurrentCountPerPage()
{
string countStr = this.comboBoxEdit1.EditValue.ToString();
//string strFilter = Regex.Replace(countStr, @"[^/d./d]", "");
string strFilter = Regex.Replace(countStr, @"[^\d+]", "");
int count = -1;//每页大小
switch (countStr)
{
case COUNT_PER_PAGE_100:
count = 100;
break;
case COUNT_PER_PAGE_10:
count = 10;
break;
case COUNT_PER_PAGE_20:
count = 20;
break;
case COUNT_PER_PAGE_50:
count = 50;
break;
case COUNT_PER_PAGE_ALL:
count = RecordCount;
break;
default:
if (!int.TryParse(countStr, out count))
{
int a;
if (int.TryParse(strFilter,out a)==true)
{
count = int.Parse(strFilter);
}
else
{
count = -1;
}
}
break;
}
return count;
}
//按下回车键进行分页操作
private void txtPageSize_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
lnkFirst_LinkClicked(null, null);
this.comboBoxEdit1.Text = "每页" + this.txtPageSize.Text + "条";
}
}
private void txtPageSize_Leave(object sender, EventArgs e)
{
string countStr = this.comboBoxEdit1.EditValue.ToString();
string strFilter = Regex.Replace(countStr, @"[^\d+]", "");
this.txtPageSize.Text = Regex.Replace(countStr, @"[^\d+]", "");
}
}
}