winform分頁控件 附源碼下載。本站提示廣大學習愛好者:(winform分頁控件 附源碼下載)文章只能為提供參考,不一定能成為您想要的結果。以下是winform分頁控件 附源碼下載正文
之前都是從事B/S開辟,因為公司有個比擬年夜的C/S項目,在應用DATAGRIDVIEW的時刻,顯示數據量比擬年夜,所以才用分頁形式,也不曉得如許能否准確。
想找個C/S上面的分頁控件,都沒有甚麼好的,就本身跟B/S下的分頁控件,修正成WINFORM上面的。
起首創立一個用戶控件稱號為pager,在控件中拖入bindingNavigator和bindingSource,修正bindingNavigator,參加需要的一些控件。
後果以下:
代碼完成以下:
namespace WindowsApp.MyControl { /**//// <summary> /// 聲名拜托 /// </summary> /// <param name="e"></param> /// <returns></returns> public delegate int EventPagingHandler(EventPagingArg e); /**//// <summary> /// 分頁控件出現 /// </summary> public partial class Pager : UserControl { public Pager() { 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 { return _pageCurrent; } set { _pageCurrent = 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 Bind() { if (this.EventPaging != null) { this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent)); } 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; } } 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 { Common.MessageProcess.ShowError("輸出數字格局毛病!"); } } } } /**//// <summary> /// 自界說事宜數據基類 /// </summary> public class EventPagingArg : EventArgs { private int _intPageIndex; public EventPagingArg(int PageIndex) { _intPageIndex = PageIndex; } } }
控件功效根本完成。
若何綁定命據呢?
年夜數目分頁,應用存儲進程。
這個存儲進程是收集上考的,呵呵。我把它給貼出來,願望原作者別砸我磚頭。
ALTER PROCEDURE SP_Pagination /**//* *************************************************************** ** 萬萬數目級分頁存儲進程 ** *************************************************************** 參數解釋: 1.Tables :表稱號,視圖 2.PrimaryKey :主症結字 3.Sort :排序語句,不帶Order By 好比:NewsID Desc,OrderRows Asc 4.CurrentPage :以後頁碼 5.PageSize :分頁尺寸 6.Filter :過濾語句,不帶Where 7.Group :Group語句,不帶Group By 後果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx ***************************************************************/ ( @Tables varchar(2000), @PrimaryKey varchar(500), @Sort varchar(500) = NULL, @CurrentPage int = 1, @PageSize int = 10, @Fields varchar(2000) = '*', @Filter varchar(1000) = NULL, @Group varchar(1000) = NULL ) AS /**//*默許排序*/ IF @Sort IS NULL OR @Sort = '' SET @Sort = @PrimaryKey DECLARE @SortTable varchar(1000) DECLARE @SortName varchar(1000) DECLARE @strSortColumn varchar(1000) DECLARE @operator char(2) DECLARE @type varchar(1000) DECLARE @prec int /**//*設定排序語句.*/ IF CHARINDEX('DESC',@Sort)>0 BEGIN SET @strSortColumn = REPLACE(@Sort, 'DESC', '') SET @operator = '<=' END ELSE BEGIN IF CHARINDEX('ASC', @Sort) = 0 SET @strSortColumn = REPLACE(@Sort, 'ASC', '') SET @operator = '>=' END IF CHARINDEX('.', @strSortColumn) > 0 BEGIN SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn)) SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn)) END ELSE BEGIN SET @SortTable = @Tables SET @SortName = @strSortColumn END SELECT @type=t.name, @prec=c.prec FROM sysobjects o JOIN syscolumns c on o.id=c.id JOIN systypes t on c.xusertype=t.xusertype WHERE o.name = @SortTable AND c.name = @SortName IF CHARINDEX('char', @type) > 0 SET @type = @type + '(' + CAST(@prec AS varchar) + ')' DECLARE @strPageSize varchar(500) DECLARE @strStartRow varchar(500) DECLARE @strFilter varchar(1000) DECLARE @strSimpleFilter varchar(1000) DECLARE @strGroup varchar(1000) /**//*默許以後頁*/ IF @CurrentPage < 1 SET @CurrentPage = 1 /**//*設置分頁參數.*/ SET @strPageSize = CAST(@PageSize AS varchar(500)) SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(500)) /**//*挑選和分組語句.*/ IF @Filter IS NOT NULL AND @Filter != '' BEGIN SET @strFilter = ' WHERE ' + @Filter + ' ' SET @strSimpleFilter = ' AND ' + @Filter + ' ' END ELSE BEGIN SET @strSimpleFilter = '' SET @strFilter = '' END IF @Group IS NOT NULL AND @Group != '' SET @strGroup = ' GROUP BY ' + @Group + ' ' ELSE SET @strGroup = '' /**//*履行查詢語句*/ EXEC( ' DECLARE @SortColumn ' + @type + ' SET ROWCOUNT ' + @strStartRow + ' SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + ' SET ROWCOUNT ' + @strPageSize + ' SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + ' ' )
應用該存儲過陳,獲得數據,將數據綁定到數據控件,供給了一個pageData類
/**//// <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 ) }; parameters[0].Value = _TableName; parameters[1].Value = _PrimaryKey; parameters[2].Value = _OrderStr; parameters[3].Value = PageIndex; parameters[4].Value = PageSize; parameters[5].Value =_QueryFieldName; parameters[6].Value = _QueryCondition; parameters[7].Value = string.Empty; DataSet ds = DbHelperSQL.RunProcedure("SP_Pagination", parameters, "dd"); _TotalCount = 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; } public int GetTotalCount() { string strSql = " select count(1) from "+_TableName; if (_QueryCondition != string.Empty) { strSql +=" where " + _QueryCondition; } return int.Parse(DbHelperSQL.GetSingle(strSql).ToString()); } }
好了,在頁面放個DATAGRIDVIEW 拖入控件pager
private void ReceiveOrderJLForm_Load(object sender, EventArgs e) { this.pager1.PageCurrent = 1; this.pager1.Bind(); } private int dgvBind() { WindowsApp.MyControl.PageData pageData = new WindowsApp.MyControl.PageData(); pageData.TableName = "T_ReceiveOrder"; pageData.PrimaryKey = "ReceiveOrderID"; pageData.OrderStr = "ReceiveOrderID desc"; pageData.PageIndex = this.pager1.PageCurrent; pageData.PageSize = this.pager1.PageSize; pageData.QueryCondition = _strSql + strWhere.ToString(); pageData.QueryFieldName = "*"; this.pager1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0]; this.pager1.bindingNavigator.BindingSource = pager1.bindingSource; dgvReceiveOrder.AutoGenerateColumns = false; dgvReceiveOrder.DataSource = this.pager1.bindingSource; return pageData.TotalCount; } private int pager1_EventPaging(WindowsApp.MyControl.EventPagingArg e) { return dgvBind(); }
後果以下
源碼下載:http://xiazai.jb51.net/201609/yuanma/winformPager(jb51.net).rar
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。