简介:DevExpress中如何实现GridControl的分页功能(组件),此文主要是在之前文章里的代码基础上重新封装成的组件
主要是利用DataNavigator和GridControl组合,自定义事件实现分页功能
接下来,我们就去实现分页功能,先看下效果图:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
整个分页操作,基本分三步:
一:界面层
二:实体层
三:代码层
四:调用
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
一:界面层,如图:
新建用户控件,命名:PageUpControl,在Designer.cs设计文件中添加如下代码:
- namespace BDSS.Component
- {
- partial class PageUpControl
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Component Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.nvgtDataPager = new DevExpress.XtraEditors.DataNavigator();
- this.SuspendLayout();
- //
- // nvgtDataPager
- //
- this.nvgtDataPager.Buttons.Append.Visible = false;
- this.nvgtDataPager.Buttons.CancelEdit.Visible = false;
- this.nvgtDataPager.Buttons.EndEdit.Visible = false;
- this.nvgtDataPager.Buttons.First.Hint = "首页";
- this.nvgtDataPager.Buttons.First.Visible = false;
- this.nvgtDataPager.Buttons.Last.Hint = "末页";
- this.nvgtDataPager.Buttons.Last.Visible = false;
- this.nvgtDataPager.Buttons.Next.Visible = false;
- this.nvgtDataPager.Buttons.NextPage.Hint = "下一页";
- this.nvgtDataPager.Buttons.NextPage.Visible = false;
- this.nvgtDataPager.Buttons.Prev.Visible = false;
- this.nvgtDataPager.Buttons.PrevPage.Hint = "上一页";
- this.nvgtDataPager.Buttons.PrevPage.Visible = false;
- this.nvgtDataPager.Buttons.Remove.Visible = false;
- this.nvgtDataPager.CustomButtons.AddRange(new DevExpress.XtraEditors.NavigatorCustomButton[] {
- new DevExpress.XtraEditors.NavigatorCustomButton(-1, 0, true, true, "首页", "首页"),
- new DevExpress.XtraEditors.NavigatorCustomButton(-1, 1, true, true, "上一页", "上一页"),
- new DevExpress.XtraEditors.NavigatorCustomButton(-1, 4, true, true, "下一页", "下一页"),
- new DevExpress.XtraEditors.NavigatorCustomButton(-1, 5, true, true, "末页", "末页")});
- this.nvgtDataPager.Dock = System.Windows.Forms.DockStyle.Fill;
- this.nvgtDataPager.Location = new System.Drawing.Point(0, 0);
- this.nvgtDataPager.Margin = new System.Windows.Forms.Padding(300, 3, 300, 3);
- this.nvgtDataPager.Name = "nvgtDataPager";
- this.nvgtDataPager.ShowToolTips = true;
- this.nvgtDataPager.Size = new System.Drawing.Size(800, 42);
- this.nvgtDataPager.TabIndex = 18;
- this.nvgtDataPager.Text = "dataNavigator1";
- this.nvgtDataPager.TextLocation = DevExpress.XtraEditors.NavigatorButtonsTextLocation.Center;
- this.nvgtDataPager.TextStringFormat = "第 {0}页 ,共 {1}";
- this.nvgtDataPager.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(this.nvgtDataPager_ButtonClick_1);
- //
- // PageUpControl
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.Controls.Add(this.nvgtDataPager);
- this.Name = "PageUpControl";
- this.Size = new System.Drawing.Size(800, 42);
- this.ResumeLayout(false);
- }
- #endregion
- private DevExpress.XtraEditors.DataNavigator nvgtDataPager;
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二:实体层
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace BDSS.Model.BLL.Model
- {
- public class SQLParams
- {
- Oracle.DataAccess.Client.OracleParameter[] _oralceParameter;
- string _sql;
- public Oracle.DataAccess.Client.OracleParameter[] OracleParameteies
- {
- get
- {
- return _oralceParameter;
- }
- set
- {
- _oralceParameter = value;
- }
- }
- public string SQL
- {
- get
- {
- return _sql;
- }
- set
- {
- _sql = value;
- }
- }
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三:代码层
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using DevExpress.XtraEditors;
- using DevExpress.XtraGrid;
- using DevExpress.XtraGrid.Views.Grid;
- using BDSS.Model.BLL.Model;
- namespace BDSS.Component
- {
- public partial class PageUpControl : DevExpress.XtraEditors.XtraUserControl
- {
- public PageUpControl()
- {
- InitializeComponent();
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
- nvgtDataPager.TextStringFormat = string.Format("");
- }
- private GridControl myControl;
- public GridControl MyControl
- {
- get { return myControl; }
- set { myControl = value; }
- }
- private int pageIndex;
- /// <summary>
- /// 当前页数
- /// </summary>
- public int PageIndex
- {
- get { return pageIndex; }
- set { pageIndex = value; }
- }
- /// <summary>
- /// 总页数
- /// </summary>
- public int PageCount = 0;
- private int pagesize;
- /// <summary>
- /// 页行数
- /// </summary>
- public int Pagesize
- {
- get { return pagesize; }
- set { pagesize = value; }
- }
- private int pageRow;
- public int PageRow
- {
- get { return pageRow; }
- set { pageRow = value; }
- }
- private int startIndex;
- private int endIndex;
- public GetMethod Method;
- public ParamMethod MyParamMethod;
- public SQLParams OracleParams;
- public string StrWhere = "";
- public string StrOrder = "";
- public void GetDataSet(GetMethod myMethod,string strwhere,string strOrder)
- {
- Method = myMethod;
- StrWhere = strwhere;
- StrOrder = strOrder;
- BindPageGridList();
- DevExpress.XtraGrid.Views.Grid.GridView gv = (GridView)MyControl.Views[0];
- //查询为空提示
- gv.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(gv_CustomDrawEmptyForeground);
- }
- public void GetDataSet(ParamMethod myMethod,SQLParams oracleParams, string strOrder)
- {
- MyParamMethod = myMethod;
- this.OracleParams = oracleParams;
- StrOrder = strOrder;
- BindPageGridList();
- DevExpress.XtraGrid.Views.Grid.GridView gv = (GridView)MyControl.Views[0];
- //查询为空提示
- gv.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(gv_CustomDrawEmptyForeground);
- }
- public void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
- {
- string s="";
- if (PageRow == 0)
- {
- s = "当前查询没有返回结果.";
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
- nvgtDataPager.TextStringFormat = string.Format("");
- }
- Font font = new Font("Tahoma", 10, FontStyle.Bold);
- Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5,
- e.Bounds.Height - 5);
- e.Graphics.DrawString(s, font, Brushes.Black, r);
- }
- private void ShowEvent(string eventString, NavigatorButtonBase button)
- {
- //string type = button.ButtonType.ToString();
- NavigatorCustomButton btn = (NavigatorCustomButton)button;
- string type = btn.Tag.ToString();
- if (type == "首页")
- {
- PageIndex = 1;
- }
- if (type == "下一页")
- {
- PageIndex++;
- }
- if (type == "末页")
- {
- PageIndex = PageCount;
- }
- if (type == "上一页")
- {
- PageIndex--;
- }
- BindPageGridList();
- }
- public void BindPageGridList()
- {
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
- //记录获取开始数
- startIndex = (PageIndex - 1) * Pagesize + 1;
- //结束数
- endIndex = PageIndex * Pagesize;
- //获取总页数
- if (PageRow % Pagesize > 0)
- {
- PageCount = PageRow / Pagesize + 1;
- }
- else
- {
- PageCount = PageRow / Pagesize;
- }
- if (PageIndex == 1)
- {
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; ;
- }
- //最后页时获取真实记录数
- if (PageCount == PageIndex)
- {
- endIndex = PageRow;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
- }
- if (true)
- {
- }
- if (MyParamMethod != null)
- {
- MyControl.DataSource = MyParamMethod(OracleParams, StrOrder, startIndex, endIndex).Tables[0];
- }
- else
- {
- MyControl.DataSource = Method(StrWhere, StrOrder, startIndex, endIndex).Tables[0];
- }
- nvgtDataPager.TextStringFormat = string.Format("第 {0}页, 共 {1}页", PageIndex, PageCount);
- }
- public delegate DataSet GetMethod(string strWhere, string orderby, int startIndex, int endIndex);
- public delegate DataSet ParamMethod(SQLParams oralceParams,string orderby, int startIndex, int endIndex);
- private void nvgtDataPager_ButtonClick_1(object sender, NavigatorButtonClickEventArgs e)
- {
- ShowEvent("ButtonClick", e.Button);
- }
- }
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
四:调用
如文章开头第一张效果图所述,当我就行查询操作时,也就是调用分页和绑定数据,需要做的操作,如下:
- pageUpControl1.MyControl = GridControl控件名称;
- pageUpControl1.PageRow =总条数;
- pageUpControl1.PageIndex = 页索引(起始页);
- pageUpControl1.Pagesize = 每页条数;
- pageUpControl1.GetDataSet(分页数据集, 查询条件,排序条件);
分页数据集:
- /// <summary>
- /// 分页获取数据列表
- /// </summary>
- public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("SELECT * FROM ( ");
- strSql.Append(" SELECT ROW_NUMBER() OVER (");
- if (!string.IsNullOrEmpty(orderby.Trim()))
- {
- strSql.Append("order by T." + orderby);
- }
- else
- {
- strSql.Append("order by T.TASK_BEGIN_DATE desc");
- }
- strSql.Append(")AS Rowss, T.ID,T.TASKORDER_ID,T.TASKORDER_CREATER,T.SATELLITE_TYPE_CODE,T.TASKORDER_TYPE_CODE,T.TASK_PRIORITY_CODE,T.RELEASE_DATE,T.RECEIVE_DATE,T.TASK_BEGIN_DATE,T.TASK_END_DATE,T.DATA_PUSH_PATH,T.AREA_CODE,T.TASKORDER_STATUS,T.TASKORDER_COMMENT.getStringVal() TASKORDER_COMMENT,T.STATUS,T.CREATE_USERID from TL_TASK_ORDER_LOG T ");
- if (!string.IsNullOrEmpty(strWhere.Trim()))
- {
- strSql.Append(" WHERE " + strWhere);
- }
- strSql.Append(" ) TT");
- strSql.AppendFormat(" WHERE TT.Rowss between {0} and {1}", startIndex, endIndex);
- return _DbHelperOra.Query(strSql.ToString());
- }
说明:此处只需要指定你自定义的条件就可以了,以上代码展示的只是文章图一的实现
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
好了,到这,我们的所有操作就完成了,来看下我们运行的分页效果图: