关于数据分页的文章太多了,各有各的一套方案,但大多都很类似,要么使用存储过程,要么直接使用代码进行分页。各种方案分页的效率也不尽相同,我们不一定要找一个最高效的(根据实际的项目情况),找一个最合适的就OK了。下面我要谈的分页控件非常灵活,可以支持任意类型的数据库,同时可以支持存储过程或代码分页(会自动判断),也支持多表的分页,非常的方便。对于数据分页的相关文章,在我的博客中可以找到很多,下面我做一个简单的汇总,方便大家查阅。
下面要给大家分享的分页控件只做分页的处理,不做与数据库相关的操作。直接提供分页的数据给分页控件即可。它不关心你的数据来源是什么,也不关心你采用的数据分页的方式(存储过程或代码等)。这个分页控件我取名为:UcPagerEx,如下图所示:
1 using System; 2 using System.ComponentModel; 3 using System.Windows.Forms; 4 namespace RDIFramework.Controls 5 { 6 public delegate void PageChangedEventHandler(object sender, EventArgs e); 7 /// 8 /// 分页用户控件,仅提供分页信息显示及改变页码操作 9 /// 10 public partial class UcPagerEx : UserControl 11 { 12 public event PageChangedEventHandler PageChanged; 13 private int _pageSize; 14 private int m_PageCount; 15 private int _recordCount; 16 private int _pageIndex; 17 18 19 public UcPagerEx() 20 { 21 InitializeComponent(); 22 this._pageSize = 10; 23 this._recordCount = 0; 24 this._pageIndex = 1; //默认为第一页 25 } 26 /// 27 /// 带参数的构造函数 28 /// 每页记录数 29 /// 总记录数 30 /// 31 public UcPagerEx(int recordCount, int pageSize) 32 { 33 InitializeComponent(); 34 35 this._pageSize = pageSize; 36 this._recordCount = recordCount; 37 this._pageIndex = 1; //默认为第一页 38 this.InitPageInfo(); 39 } 40 protected virtual void OnPageChanged(EventArgs e) 41 { 42 if (PageChanged != null) 43 { 44 InitPageInfo(); 45 PageChanged(this, e); 46 } 47 } 48 [Description("设置或获取一页中显示的记录数目"), DefaultValue(20), Category("分页")] 49 public int PageSize 50 { 51 set 52 { 53 this._pageSize = value; 54 } 55 get 56 { 57 return this._pageSize; 58 } 59 } 60 61 [Description("获取记录总页数"), DefaultValue(0), Category("分页")] 62 public int PageCount 63 { 64 get 65 { 66 return this.m_PageCount; 67 } 68 } 69 70 [Description("设置或获取记录总数"), Category("分页")] 71 public int RecordCount 72 { 73 set 74 { 75 this._recordCount = value; 76 } 77 get 78 { 79 return this._recordCount; 80 } 81 } 82 83 [Description("当前的页面索引, 开始为1"), DefaultValue(0), Category("分页")] 84 [Browsable(false)] 85 public int PageIndex 86 { 87 set 88 { 89 this._pageIndex = value; 90 } 91 get 92 { 93 return this._pageIndex; 94 } 95 } 96 97 /// 98 /// 初始化分页信息 99 /// 每页记录数100 /// 总记录数101 /// 102 public void InitPageInfo(int recordCount, int pageSize)103 {104 this._recordCount = recordCount;105 this._pageSize = pageSize;106 this.InitPageInfo();107 }108 109 /// 110 /// 初始化分页信息111 /// 总记录数112 /// 113 public void InitPageInfo(int recordCount)114 {115 this._recordCount = recordCount;116 this.InitPageInfo();117 }118 /// 119 /// 初始化分页信息120 /// 121 public void InitPageInfo()122 {123 if (this._pageSize < 1)124 this._pageSize = 10; //如果每页记录数不正确,即更改为10125 if (this._recordCount < 0)126 this._recordCount = 0; //如果记录总数不正确,即更改为0127 128 //取得总页数129 if (this._recordCount % this._pageSize == 0)130 {131 this.m_PageCount = this._recordCount / this._pageSize;132 }133 else134 {135 this.m_PageCount = this._recordCount / this._pageSize + 1;136 }137 138 //设置当前页139 if (this._pageIndex > this.m_PageCount)140 {141 this._pageIndex = this.m_PageCount;142 }143 if (this._pageIndex < 1)144 {145 this._pageIndex = 1;146 }147 148 //设置上一页按钮的可用性149 bool enable = (this.PageIndex > 1);150 this.btnPrevious.Enabled = enable;151 152 //设置首页按钮的可用性153 enable = (this.PageIndex > 1);154 this.btnFirst.Enabled = enable;155 156 //设置下一页按钮的可用性157 enable = (this.PageIndex < this.PageCount);158 this.btnNext.Enabled = enable;159 160 //设置末页按钮的可用性161 enable = (this.PageIndex < this.PageCount);162 this.btnLast.Enabled = enable;163 this.txtPageIndex.Text = this._pageIndex.ToString();164 this.lblPageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", this._recordCount, this._pageSize, this.m_PageCount);165 }166 167 public void RefreshData(int page)168 {169 this._pageIndex = page;170 EventArgs e = new EventArgs();171 OnPageChanged(e);172 }173 174 private void btnFirst_Click(object sender, System.EventArgs e)175 {176 this.RefreshData(1);177 }178 179 private void btnPrevious_Click(object sender, System.EventArgs e)180 {181 if (this._pageIndex > 1)182 {183 this.RefreshData(this._pageIndex - 1);184 }185 else186 {187 this.RefreshData(1);188 }189 }190 private void btnNext_Click(object sender, System.EventArgs e)191 {192 if (this._pageIndex < this.m_PageCount)193 {194 this.RefreshData(this._pageIndex + 1);195 }196 else if (this.m_PageCount < 1)197 {198 this.RefreshData(1);199 }200 else201 {202 this.RefreshData(this.m_PageCount);203 }204 }205 206 private void btnLast_Click(object sender, System.EventArgs e)207 {208 this.RefreshData(this.m_PageCount > 0 ? this.m_PageCount : 1);209 }210 211 private void txtPageIndex_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)212 {213 if (e.KeyCode == Keys.Enter)214 {215 int num;216 try217 {218 num = Convert.ToInt16(this.txtPageIndex.Text);219 }220 catch221 {222 num = 1;223 }224 225 if (num > this.m_PageCount)226 num = this.m_PageCount;227 if (num < 1)228 num = 1;229 230 this.RefreshData(num);231 }232 }233 }234 }
1 private void Search() 2 { 3 var recordCount = 0; 4 this.DTProductInfo = GetData(out recordCount, ucPager.PageIndex, ucPager.PageSize, this.searchValue); 5 ucPager.RecordCount = recordCount; 6 ucPager.InitPageInfo(); 7 // 加载绑定数据 8 this.GetList(); 9 }10 11 private DataTable GetData(out int recordCount, int pageIndex, int pageSize,string search)12 {13 return new ProductInfoManager(dbProvider).GetDTByPage(out recordCount, pageIndex, pageSize, search,ProductInfoTable.FieldCreateOn + " DESC ");14 }15 16 public override void GetList()17 {18 this.dgvProductInfo.AutoGenerateColumns = false;19 if (this.DTProductInfo.Columns.Count > 0)20 {21 this.DTProductInfo.DefaultView.Sort = ProductInfoTable.FieldCreateOn;22 }23 24 this.dgvProductInfo.DataSource = this.DTProductInfo.DefaultView;25 this.SetControlState();26 }
1 private void ucPager_PageChanged(object sender, EventArgs e)2 {3 var holdCursor = this.Cursor;4 this.Cursor = Cursors.WaitCursor;5 Search();6 this.Cursor = holdCursor;7 }