最近cs项目中有用到分页控件,整个系统是基于DevExpress组件开发的,由于对这个组件的布局不是很熟,也借鉴了网上一些其他人的做法,终于做了一个分页的自定义控件,控件的外观设计是参考公司网站后台日志查询分页导航的样式和图片,下面就来介绍一下具体怎么做吧。
首先新建一个UserControl
designer相关代码:
namespace Common.UI.UserControl { partial class PageControl { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PageControl)); this.layoutControl1 = new DevExpress.XtraLayout.LayoutControl(); this.lblTotalPage = new DevExpress.XtraEditors.LabelControl(); this.labelControl2 = new DevExpress.XtraEditors.LabelControl(); this.lblPageInfo = new DevExpress.XtraEditors.LabelControl(); this.txtPage = new DevExpress.XtraEditors.TextEdit(); this.btnReload = new DevExpress.XtraEditors.SimpleButton(); this.btnLastPage = new DevExpress.XtraEditors.SimpleButton(); this.btnNextPage = new DevExpress.XtraEditors.SimpleButton(); this.btnPrevPage = new DevExpress.XtraEditors.SimpleButton(); this.btnFirstPage = new DevExpress.XtraEditors.SimpleButton(); this.cmbPageSize = new DevExpress.XtraEditors.ComboBoxEdit(); this.layoutControlGroup1 = new DevExpress.XtraLayout.LayoutControlGroup(); this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem(); this.layoutControlItem11 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem2 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem12 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem13 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem18 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem3 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem4 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem5 = new DevExpress.XtraLayout.LayoutControlItem(); this.layoutControlItem6 = new DevExpress.XtraLayout.LayoutControlItem(); ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit(); this.layoutControl1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.txtPage.Properties)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.cmbPageSize.Properties)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlGroup1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem11)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem12)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem13)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem18)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem4)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).BeginInit(); this.SuspendLayout(); // // layoutControl1 // this.layoutControl1.Controls.Add(this.lblTotalPage); this.layoutControl1.Controls.Add(this.labelControl2); this.layoutControl1.Controls.Add(this.lblPageInfo); this.layoutControl1.Controls.Add(this.txtPage); this.layoutControl1.Controls.Add(this.btnReload); this.layoutControl1.Controls.Add(this.btnLastPage); this.layoutControl1.Controls.Add(this.btnNextPage); this.layoutControl1.Controls.Add(this.btnPrevPage); this.layoutControl1.Controls.Add(this.btnFirstPage); this.layoutControl1.Controls.Add(this.cmbPageSize); this.layoutControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.layoutControl1.Location = new System.Drawing.Point(0, 0); this.layoutControl1.Name = "layoutControl1"; this.layoutControl1.Root = this.layoutControlGroup1; this.layoutControl1.Size = new System.Drawing.Size(953, 49); this.layoutControl1.TabIndex = 1; this.layoutControl1.Text = "layoutControl1"; // // lblTotalPage // this.lblTotalPage.Location = new System.Drawing.Point(190, 12); this.lblTotalPage.Name = "lblTotalPage"; this.lblTotalPage.Size = new System.Drawing.Size(50, 25); this.lblTotalPage.StyleController = this.layoutControl1; this.lblTotalPage.TabIndex = 25; this.lblTotalPage.Text = "{0}"; // // labelControl2 // this.labelControl2.Location = new System.Drawing.Point(180, 12); this.labelControl2.Name = "labelControl2"; this.labelControl2.Size = new System.Drawing.Size(6, 25); this.labelControl2.StyleController = this.layoutControl1; this.labelControl2.TabIndex = 24; this.labelControl2.Text = "/"; // // lblPageInfo // this.lblPageInfo.Location = new System.Drawing.Point(831, 17); this.lblPageInfo.Name = "lblPageInfo"; this.lblPageInfo.Size = new System.Drawing.Size(110, 14); this.lblPageInfo.StyleController = this.layoutControl1; this.lblPageInfo.TabIndex = 23; this.lblPageInfo.Text = "每页{0}条,共{1}条"; // // txtPage // this.txtPage.Location = new System.Drawing.Point(126, 14); this.txtPage.Name = "txtPage"; this.txtPage.Size = new System.Drawing.Size(50, 21); this.txtPage.StyleController = this.layoutControl1; this.txtPage.TabIndex = 22; this.txtPage.TextChanged += new System.EventHandler(this.txtPage_TextChanged); this.txtPage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtPage_KeyDown); // // btnReload // this.btnReload.Image = ((System.Drawing.Image)(resources.GetObject("btnReload.Image"))); this.btnReload.Location = new System.Drawing.Point(304, 12); this.btnReload.Name = "btnReload"; this.btnReload.Size = new System.Drawing.Size(26, 25); this.btnReload.StyleController = this.layoutControl1; this.btnReload.TabIndex = 21; this.btnReload.Click += new System.EventHandler(this.btnReload_Click); // // btnLastPage // this.btnLastPage.Image = ((System.Drawing.Image)(resources.GetObject("btnLastPage.Image"))); this.btnLastPage.Location = new System.Drawing.Point(274, 12); this.btnLastPage.Name = "btnLastPage"; this.btnLastPage.Size = new System.Drawing.Size(26, 25); this.btnLastPage.StyleController = this.layoutControl1; this.btnLastPage.TabIndex = 16; this.btnLastPage.Click += new System.EventHandler(this.btnLastPage_Click); // // btnNextPage // this.btnNextPage.Image = ((System.Drawing.Image)(resources.GetObject("btnNextPage.Image"))); this.btnNextPage.Location = new System.Drawing.Point(244, 12); this.btnNextPage.Name = "btnNextPage"; this.btnNextPage.Size = new System.Drawing.Size(26, 25); this.btnNextPage.StyleController = this.layoutControl1; this.btnNextPage.TabIndex = 15; this.btnNextPage.Click += new System.EventHandler(this.btnNextPage_Click); // // btnPrevPage // this.btnPrevPage.Image = ((System.Drawing.Image)(resources.GetObject("btnPrevPage.Image"))); this.btnPrevPage.Location = new System.Drawing.Point(96, 12); this.btnPrevPage.Name = "btnPrevPage"; this.btnPrevPage.Size = new System.Drawing.Size(26, 25); this.btnPrevPage.StyleController = this.layoutControl1; this.btnPrevPage.TabIndex = 5; this.btnPrevPage.Click += new System.EventHandler(this.btnPrevPage_Click); // // btnFirstPage // this.btnFirstPage.Image = ((System.Drawing.Image)(resources.GetObject("btnFirstPage.Image"))); this.btnFirstPage.Location = new System.Drawing.Point(66, 12); this.btnFirstPage.Name = "btnFirstPage"; this.btnFirstPage.Size = new System.Drawing.Size(26, 25); this.btnFirstPage.StyleController = this.layoutControl1; this.btnFirstPage.TabIndex = 4; this.btnFirstPage.Click += new System.EventHandler(this.btnFirstPage_Click); // // cmbPageSize // this.cmbPageSize.EditValue = "20"; this.cmbPageSize.Location = new System.Drawing.Point(12, 14); this.cmbPageSize.Name = "cmbPageSize"; this.cmbPageSize.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] { new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)}); this.cmbPageSize.Properties.Items.AddRange(new object[] { "20", "50", "100", "200"}); this.cmbPageSize.Size = new System.Drawing.Size(50, 21); this.cmbPageSize.StyleController = this.layoutControl1; this.cmbPageSize.TabIndex = 14; this.cmbPageSize.SelectedIndexChanged += new System.EventHandler(this.cmbPageSize_SelectedIndexChanged); // // layoutControlGroup1 // this.layoutControlGroup1.CustomizationFormText = "Root"; this.layoutControlGroup1.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True; this.layoutControlGroup1.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] { this.emptySpaceItem1, this.layoutControlItem11, this.layoutControlItem2, this.layoutControlItem1, this.layoutControlItem12, this.layoutControlItem13, this.layoutControlItem18, this.layoutControlItem3, this.layoutControlItem4, this.layoutControlItem5, this.layoutControlItem6}); this.layoutControlGroup1.Location = new System.Drawing.Point(0, 0); this.layoutControlGroup1.Name = "Root"; this.layoutControlGroup1.Size = new System.Drawing.Size(953, 49); this.layoutControlGroup1.Text = "Root"; this.layoutControlGroup1.TextVisible = false; // // emptySpaceItem1 // this.emptySpaceItem1.AllowHotTrack = false; this.emptySpaceItem1.CustomizationFormText = "emptySpaceItem1"; this.emptySpaceItem1.Location = new System.Drawing.Point(322, 0); this.emptySpaceItem1.Name = "emptySpaceItem1"; this.emptySpaceItem1.Size = new System.Drawing.Size(411, 29); this.emptySpaceItem1.Text = "emptySpaceItem1"; this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0); // // layoutControlItem11 // this.layoutControlItem11.Control = this.cmbPageSize; this.layoutControlItem11.ControlAlignment = System.Drawing.ContentAlignment.MiddleLeft; this.layoutControlItem11.CustomizationFormText = "显示"; this.layoutControlItem11.FillControlToClientArea = false; this.layoutControlItem11.Location = new System.Drawing.Point(0, 0); this.layoutControlItem11.MaxSize = new System.Drawing.Size(54, 29); this.layoutControlItem11.MinSize = new System.Drawing.Size(54, 29); this.layoutControlItem11.Name = "layoutControlItem11"; this.layoutControlItem11.Size = new System.Drawing.Size(54, 29); this.layoutControlItem11.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem11.Text = "显示"; this.layoutControlItem11.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem11.TextToControlDistance = 0; this.layoutControlItem11.TextVisible = false; // // layoutControlItem2 // this.layoutControlItem2.Control = this.btnPrevPage; this.layoutControlItem2.CustomizationFormText = "layoutControlItem2"; this.layoutControlItem2.Location = new System.Drawing.Point(84, 0); this.layoutControlItem2.MaxSize = new System.Drawing.Size(30, 29); this.layoutControlItem2.MinSize = new System.Drawing.Size(30, 29); this.layoutControlItem2.Name = "layoutControlItem2"; this.layoutControlItem2.Size = new System.Drawing.Size(30, 29); this.layoutControlItem2.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem2.Text = "layoutControlItem2"; this.layoutControlItem2.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem2.TextToControlDistance = 0; this.layoutControlItem2.TextVisible = false; // // layoutControlItem1 // this.layoutControlItem1.Control = this.btnFirstPage; this.layoutControlItem1.CustomizationFormText = "layoutControlItem1"; this.layoutControlItem1.Location = new System.Drawing.Point(54, 0); this.layoutControlItem1.MaxSize = new System.Drawing.Size(30, 29); this.layoutControlItem1.MinSize = new System.Drawing.Size(30, 29); this.layoutControlItem1.Name = "layoutControlItem1"; this.layoutControlItem1.Size = new System.Drawing.Size(30, 29); this.layoutControlItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem1.Text = "layoutControlItem1"; this.layoutControlItem1.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem1.TextToControlDistance = 0; this.layoutControlItem1.TextVisible = false; // // layoutControlItem12 // this.layoutControlItem12.Control = this.btnNextPage; this.layoutControlItem12.CustomizationFormText = "layoutControlItem12"; this.layoutControlItem12.Location = new System.Drawing.Point(232, 0); this.layoutControlItem12.MaxSize = new System.Drawing.Size(30, 29); this.layoutControlItem12.MinSize = new System.Drawing.Size(30, 29); this.layoutControlItem12.Name = "layoutControlItem12"; this.layoutControlItem12.Size = new System.Drawing.Size(30, 29); this.layoutControlItem12.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem12.Text = "layoutControlItem12"; this.layoutControlItem12.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem12.TextToControlDistance = 0; this.layoutControlItem12.TextVisible = false; // // layoutControlItem13 // this.layoutControlItem13.Control = this.btnLastPage; this.layoutControlItem13.CustomizationFormText = "layoutControlItem13"; this.layoutControlItem13.Location = new System.Drawing.Point(262, 0); this.layoutControlItem13.MaxSize = new System.Drawing.Size(30, 29); this.layoutControlItem13.MinSize = new System.Drawing.Size(30, 29); this.layoutControlItem13.Name = "layoutControlItem13"; this.layoutControlItem13.Size = new System.Drawing.Size(30, 29); this.layoutControlItem13.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem13.Text = "layoutControlItem13"; this.layoutControlItem13.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem13.TextToControlDistance = 0; this.layoutControlItem13.TextVisible = false; // // layoutControlItem18 // this.layoutControlItem18.Control = this.btnReload; this.layoutControlItem18.CustomizationFormText = "layoutControlItem18"; this.layoutControlItem18.Location = new System.Drawing.Point(292, 0); this.layoutControlItem18.MaxSize = new System.Drawing.Size(30, 29); this.layoutControlItem18.MinSize = new System.Drawing.Size(30, 29); this.layoutControlItem18.Name = "layoutControlItem18"; this.layoutControlItem18.Size = new System.Drawing.Size(30, 29); this.layoutControlItem18.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem18.Text = "layoutControlItem18"; this.layoutControlItem18.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem18.TextToControlDistance = 0; this.layoutControlItem18.TextVisible = false; // // layoutControlItem3 // this.layoutControlItem3.Control = this.txtPage; this.layoutControlItem3.ControlAlignment = System.Drawing.ContentAlignment.MiddleLeft; this.layoutControlItem3.CustomizationFormText = "layoutControlItem3"; this.layoutControlItem3.FillControlToClientArea = false; this.layoutControlItem3.Location = new System.Drawing.Point(114, 0); this.layoutControlItem3.MaxSize = new System.Drawing.Size(54, 29); this.layoutControlItem3.MinSize = new System.Drawing.Size(54, 29); this.layoutControlItem3.Name = "layoutControlItem3"; this.layoutControlItem3.Size = new System.Drawing.Size(54, 29); this.layoutControlItem3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem3.Text = "layoutControlItem3"; this.layoutControlItem3.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem3.TextToControlDistance = 0; this.layoutControlItem3.TextVisible = false; // // layoutControlItem4 // this.layoutControlItem4.Control = this.lblPageInfo; this.layoutControlItem4.ControlAlignment = System.Drawing.ContentAlignment.MiddleRight; this.layoutControlItem4.CustomizationFormText = "layoutControlItem4"; this.layoutControlItem4.FillControlToClientArea = false; this.layoutControlItem4.Location = new System.Drawing.Point(733, 0); this.layoutControlItem4.MaxSize = new System.Drawing.Size(200, 29); this.layoutControlItem4.MinSize = new System.Drawing.Size(200, 29); this.layoutControlItem4.Name = "layoutControlItem4"; this.layoutControlItem4.Size = new System.Drawing.Size(200, 29); this.layoutControlItem4.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem4.Text = "layoutControlItem4"; this.layoutControlItem4.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem4.TextToControlDistance = 0; this.layoutControlItem4.TextVisible = false; // // layoutControlItem5 // this.layoutControlItem5.Control = this.labelControl2; this.layoutControlItem5.CustomizationFormText = "layoutControlItem5"; this.layoutControlItem5.Location = new System.Drawing.Point(168, 0); this.layoutControlItem5.MaxSize = new System.Drawing.Size(10, 29); this.layoutControlItem5.MinSize = new System.Drawing.Size(1, 29); this.layoutControlItem5.Name = "layoutControlItem5"; this.layoutControlItem5.Size = new System.Drawing.Size(10, 29); this.layoutControlItem5.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem5.Text = "layoutControlItem5"; this.layoutControlItem5.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem5.TextToControlDistance = 0; this.layoutControlItem5.TextVisible = false; // // layoutControlItem6 // this.layoutControlItem6.Control = this.lblTotalPage; this.layoutControlItem6.ControlAlignment = System.Drawing.ContentAlignment.MiddleLeft; this.layoutControlItem6.CustomizationFormText = "layoutControlItem6"; this.layoutControlItem6.Location = new System.Drawing.Point(178, 0); this.layoutControlItem6.MaxSize = new System.Drawing.Size(54, 29); this.layoutControlItem6.MinSize = new System.Drawing.Size(54, 29); this.layoutControlItem6.Name = "layoutControlItem6"; this.layoutControlItem6.Size = new System.Drawing.Size(54, 29); this.layoutControlItem6.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom; this.layoutControlItem6.Text = "layoutControlItem6"; this.layoutControlItem6.TextSize = new System.Drawing.Size(0, 0); this.layoutControlItem6.TextToControlDistance = 0; this.layoutControlItem6.TextVisible = false; // // PageControl // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.layoutControl1); this.Name = "PageControl"; this.Size = new System.Drawing.Size(953, 49); ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit(); this.layoutControl1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.txtPage.Properties)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.cmbPageSize.Properties)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlGroup1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem11)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem12)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem13)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem18)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem4)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem5)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem6)).EndInit(); this.ResumeLayout(false); } #endregion private DevExpress.XtraLayout.LayoutControl layoutControl1; private DevExpress.XtraEditors.SimpleButton btnReload; private DevExpress.XtraEditors.SimpleButton btnLastPage; private DevExpress.XtraEditors.SimpleButton btnNextPage; private DevExpress.XtraEditors.SimpleButton btnPrevPage; private DevExpress.XtraEditors.SimpleButton btnFirstPage; private DevExpress.XtraEditors.ComboBoxEdit cmbPageSize; private DevExpress.XtraLayout.LayoutControlGroup layoutControlGroup1; private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem11; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem2; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem12; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem13; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem18; private DevExpress.XtraEditors.LabelControl lblPageInfo; private DevExpress.XtraEditors.TextEdit txtPage; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem3; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem4; private DevExpress.XtraEditors.LabelControl lblTotalPage; private DevExpress.XtraEditors.LabelControl labelControl2; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem5; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem6; } }
打开设计器看下效果:
控件相关代码:
using System; using System.Windows.Forms; using DevExpress.XtraEditors; namespace Common.UI.UserControl { public partial class PageControl : XtraUserControl { /// <summary> /// 每页记录数 /// </summary> public int PageSize { get; set; } = 20; /// <summary> /// 当前页码 /// </summary> public int PageNo { get; set; } = 1; /// <summary> /// 总记录数 /// </summary> public int RecordCount { get; set; } /// <summary> /// 总页数 /// </summary> public int PageCount { get { var pageCount = 0; if (PageSize != 0) { pageCount = RecordCount / PageSize + (RecordCount % PageSize == 0 ? 0 : 1); } return pageCount; } } public PageControl() { InitializeComponent(); DoDrawControl(false); } /// <summary> /// 页面变化 /// </summary> public event EventHandler OnPageChanged; /// <summary> /// 外部调用 /// </summary> /// <param name="count">记录总数</param> public void DoDrawControl(int count) { RecordCount = count; DoDrawControl(false); } /// <summary> /// 页面变化 /// </summary> /// <param name="callEvent"></param> private void DoDrawControl(bool callEvent) { txtPage.Text = PageNo.ToString(); lblTotalPage.Text = PageCount.ToString(); lblPageInfo.Text = $"每页 {PageSize} 条, 共 {RecordCount} 条"; if (callEvent) { OnPageChanged?.Invoke(this, null);//当前分页数字改变时,触发委托事件 } SetButtonEnabled(); } /// <summary> /// 设置按钮状态 /// </summary> private void SetButtonEnabled() { txtPage.Text = PageNo.ToString(); //只有一页 if (PageCount <= 1) { btnFirstPage.Enabled = false; btnPrevPage.Enabled = false; btnNextPage.Enabled = false; btnLastPage.Enabled = false; btnReload.Enabled = false; } //第一页 else if (PageNo == 1) { btnFirstPage.Enabled = false; btnPrevPage.Enabled = false; btnNextPage.Enabled = true; btnLastPage.Enabled = true; btnReload.Enabled = true; } //最后一页 else if (PageNo == PageCount) { btnFirstPage.Enabled = true; btnPrevPage.Enabled = true; btnNextPage.Enabled = false; btnLastPage.Enabled = false; btnReload.Enabled = true; } else { btnFirstPage.Enabled = true; btnPrevPage.Enabled = true; btnNextPage.Enabled = true; btnLastPage.Enabled = true; btnReload.Enabled = true; } } private void cmbPageSize_SelectedIndexChanged(object sender, EventArgs e) { int pageSize; int.TryParse(cmbPageSize.Text.Trim(), out pageSize); PageSize = pageSize; DoDrawControl(true); } /// <summary> /// 第一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFirstPage_Click(object sender, EventArgs e) { PageNo = 1; DoDrawControl(true); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrevPage_Click(object sender, EventArgs e) { PageNo = Math.Max(1, PageNo - 1); DoDrawControl(true); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNextPage_Click(object sender, EventArgs e) { PageNo = Math.Min(PageCount, PageNo + 1); DoDrawControl(true); } /// <summary> /// 最后一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLastPage_Click(object sender, EventArgs e) { PageNo = PageCount; DoDrawControl(true); } /// <summary> /// 跳转 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReload_Click(object sender, EventArgs e) { int num; int.TryParse(txtPage.Text.Trim(), out num); if (num > 0) { PageNo = num; DoDrawControl(true); } } /// <summary> /// 添加跳转页数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtPage_TextChanged(object sender, EventArgs e) { int num; int.TryParse(txtPage.Text.Trim(), out num); if (num > 0) { if (num > PageCount) { txtPage.Text = PageCount.ToString(); } } } private void txtPage_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter) { int num; int.TryParse(txtPage.Text.Trim(), out num); if (num > 0) { if (num > PageCount) { num = PageCount; } PageNo = num; DoDrawControl(true); } } } } }
基本上到此,分页控件的UI功能就已经实现了,剩下的是sql底层分页的方法,我们先UI模拟一下,看看外部怎么调用:
public Form1() { InitializeComponent(); pageControl1.OnPageChanged += pageControl1_OnPageChanged; } private void btnSearch_Click(object sender, EventArgs e) { //获取底层数据 var response = bll.Get((pageControl1.PageNo - 1)*pageControl1.PageSize, pageControl1.PageSize); dataGridView1.DataSource = response.Item; pageControl1.DoDrawControl(response.TotalRecord); } private void pageControl1_OnPageChanged(object sender, EventArgs e) { //获取底层数据 var response = bll.Get((pageControl1.PageNo - 1)*pageControl1.PageSize, pageControl1.PageSize); dataGridView1.DataSource = response.Item; pageControl1.DoDrawControl(response.TotalRecord); }
最后说下,底层sql方面建议使用row_number(),性能和效率测评下来是最高的,如果表的主键是int的话,建议根据主键倒序排序,如果主键是guid类型的话,建议根据创建时间和主键倒序排列
--int主键 select * from (select row_number()over(order by id desc)rownumber,* from tablename)a where rownumber between 1 and 20 --guid主键 select * from (select row_number()over(order by createtime desc,id desc)rownumber,* from tablename)a where rownumber between 1 and 20
运行后就是下面这样: