/// <summary>
/// 分页功能
/// </summary>
private int pageIndex = 1;
/// <summary>
/// 当前页数
/// </summary>
public virtual int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
private int pageSize = 10;
/// <summary>
/// 每页记录数
/// </summary>
public virtual int PageSize
{
get { return int.Parse(cboPageSize.Text); ; }
set { pageSize = value; }
}
private int totalCount = 0;
/// <summary>
/// 总记录数
/// </summary>
public virtual int TotalCount
{
get { return totalCount; }
set { totalCount = value; }
}
private int pageCount = 0;
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
if (PageSize != 0)
{
if (TotalCount <= PageSize)
{
pageCount = 1;
}
else
{
pageCount = GetPageCount();
}
}
else
{
pageCount = 0;
}
return pageCount;
}
}
/// <summary>
/// 计算总页数
/// </summary>
/// <returns></returns>
private int GetPageCount()
{
int pageCount = TotalCount / PageSize;
if (TotalCount % PageSize == 0)
{
pageCount = TotalCount / PageSize;
}
else
{
pageCount = TotalCount / PageSize + 1;
}
return pageCount;
}
/// <summary>
/// 分页控制的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetFormCtrEnabled()
{
btnFirst.Enabled = true;
btnPrevious.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
btnGo.Enabled = true;
}
public void DrawControl()
{
dataGridView2.AutoGenerateColumns = false;//设定列不能自动作成
string name = txtName1.Text.Trim();
string phone = txtPhone1.Text.Trim();
string idcard = txtIDcard.Text.Trim();
string address = txtAddress.Text.Trim();
CustomerSearchVO vo = new CustomerSearchVO();
vo.Name = name;
vo.Phone = phone;
vo.IDCard = idcard;
vo.Address = address;
IList<Customer> page = bll.GetPage(vo, PageIndex, PageSize, out totalCount);
//dataGridView2.DataSource = null;
this.dataGridView2.DataSource = page;
GetPageCount();
SetFormCtrEnabled();
lblCurrentPage.Text = "第 " + PageIndex + " 页 / 共 " + PageCount + " 页";
if (PageCount == 1)//有且仅有一页时
{
btnFirst.Enabled = false;
btnPrevious.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
btnGo.Enabled = false;
}
else if (PageIndex == 1)//当前页为第一页时
{
btnFirst.Enabled = false;
btnPrevious.Enabled = false;
}
else if (PageIndex == PageCount)//当前页为最后一页时
{
btnNext.Enabled = false;
btnLast.Enabled = false;
}
dataGridView2.Columns[4].Visible = false;
dataGridView2.Columns[5].Visible = false;
dataGridView2.Columns[6].Visible = false;
dataGridView2.Columns[8].Visible = false;
}
/// <summary>
/// 首页按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btnFirst_Click(object sender, EventArgs e)
{
PageIndex = 1;
DrawControl();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrevious_Click(object sender, EventArgs e)
{
PageIndex = Math.Max(1, PageIndex - 1);
DrawControl();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + 1);
DrawControl();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl();
}
/// <summary>
/// 按下enter实现跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtPageNum_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)//如果输入的是回车键
{
this.btnGo_Click(sender, e);//触发button事件
}
}
/// <summary>
/// 跳转页数设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtPageNum_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{ //TryParse 函数,将字符串转换成等效的整数,返回bool型,判断是否转换成功。
//输入除数字以外的字符是转换不成功的
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();
}
}