123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- 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 System.Data.SqlClient;
- namespace LYFZ.ManagementService.UCPager
- {
- /// <summary>
- /// 申明委托
- /// </summary>
- /// <param name="e"></param>
- /// <returns></returns>
- public delegate int EventPagingHandler(EventPagingArg e);
- public partial class ucPagerEx : UserControl
- {
- public ucPagerEx()
- {
- InitializeComponent();
- }
- public event EventPagingHandler EventPaging;
- /// <summary>
- /// 每页显示记录数
- /// </summary>
- private int _pageSize = 20;
- /// <summary>
- /// 每页显示记录数
- /// </summary>
- public int PageSize
- {
- get { return _pageSize; }
- set
- {
- _pageSize = value;
- GetPageCount();
- }
- }
- private int _nMax = 0;
- /// <summary>
- /// 总记录数
- /// </summary>
- public int NMax
- {
- get { return _nMax; }
- set
- {
- _nMax = value;
- GetPageCount();
- }
- }
- private int _pageCount = 0;
- /// <summary>
- /// 页数=总记录数/每页显示记录数
- /// </summary>
- public int PageCount
- {
- get { return _pageCount; }
- set { _pageCount = value; }
- }
- private int _pageCurrent = 0;
- /// <summary>
- /// 当前页号
- /// </summary>
- public int PageCurrent
- {
- get {
- if (_pageCurrent <= 0) {
- _pageCurrent = 1;
- }
- return _pageCurrent; }
- set { _pageCurrent = value; }
- }
- DataTable tbDataSource = null;
- /// <summary>
- /// 分页数据源
- /// </summary>
- public DataTable TbDataSource
- {
- get { return tbDataSource; }
- set { tbDataSource = value; }
- }
- private void GetPageCount()
- {
- if (this.NMax > 0)
- {
- this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
- }
- else
- {
- this.PageCount = 0;
- }
- }
- /// <summary>
- /// 重新刷新邦定
- /// </summary>
- public void RefreshBind()
- {
- this._pageCurrent = 1;
- this.Bind();
- }
- /// <summary>
- /// 翻页控件数据绑定的方法
- /// </summary>
- public void Bind()
- {
- if (this.EventPaging != null)
- {
- this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
- }
- if (this.PageCurrent <=0)
- {
- this.PageCurrent = 1;
- }
- if (this.PageCurrent > this.PageCount)
- {
- this.PageCurrent = this.PageCount;
- }
- if (this.PageCount == 1)
- {
- this.PageCurrent = 1;
- }
- lblPageCount.Text = this.PageCount.ToString();
- this.lblMaxPage.Text = "共" + this.NMax.ToString() + "条记录";
- this.txtCurrentPage.Text = this.PageCurrent.ToString();
- if (this.PageCurrent == 1)
- {
- this.btnPrev.Enabled = false;
- this.btnFirst.Enabled = false;
- }
- else
- {
- btnPrev.Enabled = true;
- btnFirst.Enabled = true;
- }
- if (this.PageCurrent == this.PageCount)
- {
- this.btnLast.Enabled = false;
- this.btnNext.Enabled = false;
- }
- else
- {
- btnLast.Enabled = true;
- btnNext.Enabled = true;
- }
- if (this.NMax == 0)
- {
- btnNext.Enabled = false;
- btnLast.Enabled = false;
- btnFirst.Enabled = false;
- btnPrev.Enabled = false;
- }
- DataTable dtble = TbDataSource;
- if (dtble != null)
- {
- this.lblthisPageCount.Text = dtble.Rows.Count.ToString();
- }
- }
- private void btnFirst_Click(object sender, EventArgs e)
- {
- PageCurrent = 1;
- this.Bind();
- }
- private void btnPrev_Click(object sender, EventArgs e)
- {
- PageCurrent -= 1;
- if (PageCurrent <= 0)
- {
- PageCurrent = 1;
- }
- this.Bind();
- }
- private void btnNext_Click(object sender, EventArgs e)
- {
- this.PageCurrent += 1;
- if (PageCurrent > PageCount)
- {
- PageCurrent = PageCount;
- }
- this.Bind();
- }
- private void btnLast_Click(object sender, EventArgs e)
- {
- PageCurrent = PageCount;
- this.Bind();
- }
- private void btnGo_Click(object sender, EventArgs e)
- {
- if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "")
- {
- if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
- {
- this.Bind();
- }
- else
- {
- MessageBoxCustom.Show("输入数字格式错误!");
- }
- }
- }
- private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- this.Bind();
- }
- }
- private void lblbtnShowAll_Click(object sender, EventArgs e)
- {
- if (this.NMax > 10000)
- {
- if (MessageBoxCustom.Show("超过万条数据加载全部会影响软件性能,是否要显示全部数据?", msgBoxButton: MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- this.PageSize = this.NMax;
- this.PageCurrent = 1;
- this.Bind();
- }
- }
- else
- {
- this.PageSize = this.NMax;
- this.PageCurrent = 1;
- this.Bind();
- }
- }
- }
- /// <summary>
- /// 自定义事件数据基类
- /// </summary>
- public class EventPagingArg : EventArgs
- {
- private int _intPageIndex;
- public EventPagingArg(int PageIndex)
- {
- _intPageIndex = PageIndex;
- }
- }
- /// <summary>
- /// 数据源提供
- /// </summary>
- public class PageData
- {
- private int _PageSize = 10;
- private int _PageIndex = 1;
- private int _PageCount = 0;
- private int _TotalCount = 0;
- private string _TableName;//表名
- private string _QueryFieldName = "*";//表字段FieldStr
- private string _OrderStr = string.Empty; //排序_SortStr
- private string _QueryCondition = string.Empty;//查询的条件 RowFilter
- private string _PrimaryKey = string.Empty;//主键
- /// <summary>
- /// 显示页数
- /// </summary>
- public int PageSize
- {
- get
- {
- return _PageSize;
- }
- set
- {
- _PageSize = value;
- }
- }
- /// <summary>
- /// 当前页
- /// </summary>
- public int PageIndex
- {
- get
- {
- return _PageIndex;
- }
- set
- {
- _PageIndex = value;
- }
- }
- /// <summary>
- /// 总页数
- /// </summary>
- public int PageCount
- {
- get
- {
- return _PageCount;
- }
- }
- /// <summary>
- /// 总记录数
- /// </summary>
- public int TotalCount
- {
- get
- {
- return _TotalCount;
- }
- }
- /// <summary>
- /// 表名,包括视图
- /// </summary>
- public string TableName
- {
- get
- {
- return _TableName;
- }
- set
- {
- _TableName = value;
- }
- }
- /// <summary>
- /// 表字段FieldStr
- /// </summary>
- public string QueryFieldName
- {
- get
- {
- return _QueryFieldName;
- }
- set
- {
- _QueryFieldName = value;
- }
- }
- /// <summary>
- /// 排序字段
- /// </summary>
- public string OrderStr
- {
- get
- {
- return _OrderStr;
- }
- set
- {
- _OrderStr = value;
- }
- }
- /// <summary>
- /// 查询条件
- /// </summary>
- public string QueryCondition
- {
- get
- {
- return _QueryCondition;
- }
- set
- {
- _QueryCondition = value;
- }
- }
- /// <summary>
- /// 主键
- /// </summary>
- public string PrimaryKey
- {
- get
- {
- return _PrimaryKey;
- }
- set
- {
- _PrimaryKey = value;
- }
- }
- public DataSet QueryDataTable()
- {
- //SqlParameter[] parameters = {
- // new SqlParameter("@Tables", SqlDbType.VarChar, 255),
- // new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
- // new SqlParameter("@Sort", SqlDbType.VarChar , 255 ),
- // new SqlParameter("@CurrentPage", SqlDbType.Int),
- // new SqlParameter("@PageSize", SqlDbType.Int),
- // new SqlParameter("@Fields", SqlDbType.VarChar, 255),
- // new SqlParameter("@Filter", SqlDbType.VarChar,1000),
- // new SqlParameter("@Group" ,SqlDbType.VarChar , 1000 )
- // };
- //@TableName = N'dbo.tb_Customer',
- // @ReFieldsStr = N'*',
- // @OrderString = N'ShopName desc',
- // @WhereString = N'1=1',
- // @PageSize = 200,
- // @PageIndex = 10,
- // @TotalRecord = @TotalRecord OUTPUT
- SqlParameter[] parameters = {
- new SqlParameter("@TableName", SqlDbType.VarChar , 2000),
- new SqlParameter("@ReFieldsStr" , SqlDbType.VarChar , 2000),
- new SqlParameter("@OrderString", SqlDbType.VarChar , 2000 ),
- new SqlParameter("@WhereString", SqlDbType.VarChar, 2000),
- new SqlParameter("@PageSize", SqlDbType.Int),
- new SqlParameter("@PageIndex", SqlDbType.Int),
- new SqlParameter("@TotalRecord", SqlDbType.Int)
- };
- parameters[0].Value =_TableName ;
- parameters[1].Value = _QueryFieldName;
- parameters[2].Value = _OrderStr;
- parameters[3].Value = _QueryCondition;
- parameters[4].Value = PageSize;
- parameters[5].Value = PageIndex;
- parameters[6].Direction = ParameterDirection.Output;
- DataSet ds = LYFZ.Helper.SQLHelper.RunProcedure("PROCE_SQL2005PAGECHANGE", parameters, _TableName + "_ds");
- int tempRecord = Convert.ToInt32(parameters[6].Value);
- _TotalCount = tempRecord;//GetTotalCount();
- if (_TotalCount == 0)
- {
- _PageIndex = 0;
- _PageCount = 0;
- }
- else
- {
- _PageCount = _TotalCount % _PageSize == 0 ? _TotalCount / _PageSize : _TotalCount / _PageSize + 1;
- if (_PageIndex > _PageCount)
- {
- _PageIndex = _PageCount;
- parameters[4].Value = _PageSize;
- ds = QueryDataTable();
- }
- }
- return ds;
- }
- /// <summary>
- /// 获取总记录数
- /// </summary>
- /// <returns></returns>
- public int GetTotalCount()
- {
- string strSql = " select count(1) from " + _TableName;
- if (_QueryCondition != string.Empty)
- {
- strSql += " where " + _QueryCondition;
- }
- return int.Parse(LYFZ.Helper.SQLHelper.GetSingle(strSql).ToString());
- }
- }
- }
|