DevExpress中如何实现GridControl的分页功能(组件)

简介:DevExpress中如何实现GridControl的分页功能(组件),此文主要是在之前文章里的代码基础上重新封装成的组件

        主要是利用DataNavigator和GridControl组合,自定义事件实现分页功能

        接下来,我们就去实现分页功能,先看下效果图:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

整个分页操作,基本分三步:

一:界面层

二:实体层

三:代码层

四:调用

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

一:界面层,如图:

 

新建用户控件,命名:PageUpControl,在Designer.cs设计文件中添加如下代码:

[csharp]  view plain  copy
  1. namespace BDSS.Component  
  2. {  
  3.     partial class PageUpControl  
  4.     {  
  5.         /// <summary>   
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>   
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Component Designer generated code  
  24.   
  25.         /// <summary>   
  26.         /// Required method for Designer support - do not modify   
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.nvgtDataPager = new DevExpress.XtraEditors.DataNavigator();  
  32.             this.SuspendLayout();  
  33.             //   
  34.             // nvgtDataPager  
  35.             //   
  36.             this.nvgtDataPager.Buttons.Append.Visible = false;  
  37.             this.nvgtDataPager.Buttons.CancelEdit.Visible = false;  
  38.             this.nvgtDataPager.Buttons.EndEdit.Visible = false;  
  39.             this.nvgtDataPager.Buttons.First.Hint = "首页";  
  40.             this.nvgtDataPager.Buttons.First.Visible = false;  
  41.             this.nvgtDataPager.Buttons.Last.Hint = "末页";  
  42.             this.nvgtDataPager.Buttons.Last.Visible = false;  
  43.             this.nvgtDataPager.Buttons.Next.Visible = false;  
  44.             this.nvgtDataPager.Buttons.NextPage.Hint = "下一页";  
  45.             this.nvgtDataPager.Buttons.NextPage.Visible = false;  
  46.             this.nvgtDataPager.Buttons.Prev.Visible = false;  
  47.             this.nvgtDataPager.Buttons.PrevPage.Hint = "上一页";  
  48.             this.nvgtDataPager.Buttons.PrevPage.Visible = false;  
  49.             this.nvgtDataPager.Buttons.Remove.Visible = false;  
  50.             this.nvgtDataPager.CustomButtons.AddRange(new DevExpress.XtraEditors.NavigatorCustomButton[] {  
  51.             new DevExpress.XtraEditors.NavigatorCustomButton(-1, 0, truetrue"首页""首页"),  
  52.             new DevExpress.XtraEditors.NavigatorCustomButton(-1, 1, truetrue"上一页""上一页"),  
  53.             new DevExpress.XtraEditors.NavigatorCustomButton(-1, 4, truetrue"下一页""下一页"),  
  54.             new DevExpress.XtraEditors.NavigatorCustomButton(-1, 5, truetrue"末页""末页")});  
  55.             this.nvgtDataPager.Dock = System.Windows.Forms.DockStyle.Fill;  
  56.             this.nvgtDataPager.Location = new System.Drawing.Point(0, 0);  
  57.             this.nvgtDataPager.Margin = new System.Windows.Forms.Padding(300, 3, 300, 3);  
  58.             this.nvgtDataPager.Name = "nvgtDataPager";  
  59.             this.nvgtDataPager.ShowToolTips = true;  
  60.             this.nvgtDataPager.Size = new System.Drawing.Size(800, 42);  
  61.             this.nvgtDataPager.TabIndex = 18;  
  62.             this.nvgtDataPager.Text = "dataNavigator1";  
  63.             this.nvgtDataPager.TextLocation = DevExpress.XtraEditors.NavigatorButtonsTextLocation.Center;  
  64.             this.nvgtDataPager.TextStringFormat = "第 {0}页 ,共 {1}";  
  65.             this.nvgtDataPager.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(this.nvgtDataPager_ButtonClick_1);  
  66.             //   
  67.             // PageUpControl  
  68.             //   
  69.             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);  
  70.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  71.             this.Controls.Add(this.nvgtDataPager);  
  72.             this.Name = "PageUpControl";  
  73.             this.Size = new System.Drawing.Size(800, 42);  
  74.             this.ResumeLayout(false);  
  75.   
  76.         }  
  77.  
  78.         #endregion  
  79.   
  80.         private DevExpress.XtraEditors.DataNavigator nvgtDataPager;  
  81.     }  
  82. }  
说明:本文用户控件是在 BDSS.Component 命名空间下创建的

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二:实体层 

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace BDSS.Model.BLL.Model  
  7. {  
  8.     public class SQLParams  
  9.     {  
  10.         Oracle.DataAccess.Client.OracleParameter[] _oralceParameter;  
  11.         string _sql;  
  12.         public Oracle.DataAccess.Client.OracleParameter[] OracleParameteies  
  13.         {  
  14.             get   
  15.             {  
  16.                 return _oralceParameter;  
  17.             }  
  18.             set  
  19.             {  
  20.                 _oralceParameter = value;  
  21.             }  
  22.         }  
  23.         public string SQL  
  24.         {  
  25.             get  
  26.             {  
  27.                 return _sql;  
  28.             }  
  29.             set  
  30.             {  
  31.                 _sql = value;  
  32.             }  
  33.         }  
  34.     }  
  35. }  
说明:此类是在BDSS.Model.BLL.Model命名空间下创建的

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

三:代码层

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using DevExpress.XtraEditors;  
  9. using DevExpress.XtraGrid;  
  10. using DevExpress.XtraGrid.Views.Grid;  
  11. using BDSS.Model.BLL.Model;  
  12.   
  13. namespace BDSS.Component  
  14. {  
  15.   
  16.     public partial class PageUpControl : DevExpress.XtraEditors.XtraUserControl  
  17.     {  
  18.         public PageUpControl()  
  19.         {  
  20.             InitializeComponent();  
  21.             nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;  
  22.             nvgtDataPager.Buttons.CustomButtons[1].Enabled = false;  
  23.             nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;  
  24.             nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;  
  25.             nvgtDataPager.TextStringFormat = string.Format("");  
  26.         }  
  27.   
  28.    
  29.   
  30.         private GridControl myControl;  
  31.   
  32.         public GridControl MyControl  
  33.         {  
  34.             get { return myControl; }  
  35.             set { myControl = value; }  
  36.         }  
  37.   
  38.           
  39.         private int pageIndex;  
  40.         /// <summary>  
  41.         /// 当前页数  
  42.         /// </summary>  
  43.         public int PageIndex  
  44.         {  
  45.             get { return pageIndex; }  
  46.             set { pageIndex = value; }  
  47.         }  
  48.   
  49.   
  50.         /// <summary>  
  51.         /// 总页数  
  52.         /// </summary>  
  53.         public int PageCount = 0;  
  54.   
  55.   
  56.         private int pagesize;  
  57.         /// <summary>  
  58.         /// 页行数  
  59.         /// </summary>  
  60.         public int Pagesize  
  61.         {  
  62.             get { return pagesize; }  
  63.             set { pagesize = value; }  
  64.         }  
  65.   
  66.         private int pageRow;  
  67.   
  68.         public int PageRow  
  69.         {  
  70.             get { return pageRow; }  
  71.             set { pageRow = value; }  
  72.         }  
  73.   
  74.         private int startIndex;  
  75.         private int endIndex;  
  76.   
  77.         public GetMethod Method;  
  78.         public ParamMethod MyParamMethod;  
  79.         public SQLParams OracleParams;  
  80.         public string StrWhere = "";  
  81.         public string StrOrder = "";  
  82.   
  83.               
  84.           
  85.   
  86.   
  87.         public void GetDataSet(GetMethod myMethod,string strwhere,string strOrder)  
  88.         {  
  89.             Method = myMethod;  
  90.             StrWhere = strwhere;  
  91.             StrOrder = strOrder;  
  92.             BindPageGridList();  
  93.   
  94.               
  95.             DevExpress.XtraGrid.Views.Grid.GridView gv = (GridView)MyControl.Views[0];  
  96.             //查询为空提示  
  97.             gv.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(gv_CustomDrawEmptyForeground);  
  98.         }  
  99.         public void GetDataSet(ParamMethod myMethod,SQLParams oracleParams, string strOrder)  
  100.         {  
  101.             MyParamMethod = myMethod;  
  102.             this.OracleParams = oracleParams;  
  103.             StrOrder = strOrder;  
  104.             BindPageGridList();  
  105.             DevExpress.XtraGrid.Views.Grid.GridView gv = (GridView)MyControl.Views[0];  
  106.             //查询为空提示  
  107.             gv.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(gv_CustomDrawEmptyForeground);  
  108.         }  
  109.   
  110.   
  111.         public void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)  
  112.         {  
  113.             string s="";  
  114.             if (PageRow == 0)  
  115.             {  
  116.                 s = "当前查询没有返回结果.";  
  117.                 nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;  
  118.                 nvgtDataPager.Buttons.CustomButtons[1].Enabled = false;  
  119.                 nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;  
  120.                 nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;  
  121.                 nvgtDataPager.TextStringFormat = string.Format("");  
  122.             }  
  123.        
  124.             Font font = new Font("Tahoma", 10, FontStyle.Bold);  
  125.             Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5,  
  126.               e.Bounds.Height - 5);  
  127.             e.Graphics.DrawString(s, font, Brushes.Black, r);  
  128.         }  
  129.   
  130.         private void ShowEvent(string eventString, NavigatorButtonBase button)  
  131.         {  
  132.             //string type = button.ButtonType.ToString();  
  133.             NavigatorCustomButton btn = (NavigatorCustomButton)button;  
  134.             string type = btn.Tag.ToString();  
  135.             if (type == "首页")  
  136.             {  
  137.                 PageIndex = 1;  
  138.   
  139.             }  
  140.   
  141.             if (type == "下一页")  
  142.             {  
  143.                 PageIndex++;    
  144.             }  
  145.   
  146.             if (type == "末页")  
  147.             {  
  148.                 PageIndex = PageCount;  
  149.             }  
  150.   
  151.             if (type == "上一页")  
  152.             {  
  153.                 PageIndex--;     
  154.             }  
  155.             BindPageGridList();  
  156.         }  
  157.   
  158.         public void BindPageGridList()  
  159.         {  
  160.             nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;  
  161.             nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;  
  162.             nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;  
  163.             nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;  
  164.             //记录获取开始数  
  165.             startIndex = (PageIndex - 1) * Pagesize + 1;  
  166.             //结束数  
  167.             endIndex = PageIndex * Pagesize;  
  168.   
  169.             //获取总页数    
  170.             if (PageRow % Pagesize > 0)  
  171.             {  
  172.                 PageCount = PageRow / Pagesize + 1;  
  173.             }  
  174.             else  
  175.             {  
  176.                 PageCount = PageRow / Pagesize;  
  177.             }  
  178.   
  179.             if (PageIndex == 1)  
  180.             {  
  181.                 nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;  
  182.                 nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; ;  
  183.             }  
  184.   
  185.             //最后页时获取真实记录数  
  186.             if (PageCount == PageIndex)  
  187.             {  
  188.                 endIndex = PageRow;  
  189.                 nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;  
  190.                 nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;  
  191.             }  
  192.   
  193.             if (true)  
  194.             {  
  195.                   
  196.             }  
  197.             if (MyParamMethod != null)  
  198.             {  
  199.                 MyControl.DataSource = MyParamMethod(OracleParams, StrOrder, startIndex, endIndex).Tables[0];  
  200.             }  
  201.             else  
  202.             {  
  203.                 MyControl.DataSource = Method(StrWhere, StrOrder, startIndex, endIndex).Tables[0];  
  204.             }  
  205.             nvgtDataPager.TextStringFormat = string.Format("第 {0}页, 共 {1}页", PageIndex, PageCount);  
  206.              
  207.         }  
  208.   
  209.         public delegate DataSet GetMethod(string strWhere, string orderby, int startIndex, int endIndex);  
  210.         public delegate DataSet ParamMethod(SQLParams oralceParams,string orderby, int startIndex, int endIndex);  
  211.         private void nvgtDataPager_ButtonClick_1(object sender, NavigatorButtonClickEventArgs e)  
  212.         {  
  213.             ShowEvent("ButtonClick", e.Button);  
  214.         }  
  215.          
  216.     }  
  217.       
  218. }  

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

四:调用

如文章开头第一张效果图所述,当我就行查询操作时,也就是调用分页和绑定数据,需要做的操作,如下:

[csharp]  view plain  copy
  1. pageUpControl1.MyControl = GridControl控件名称;  
  2. pageUpControl1.PageRow =总条数;  
  3. pageUpControl1.PageIndex = 页索引(起始页);  
  4. pageUpControl1.Pagesize = 每页条数;  
  5. pageUpControl1.GetDataSet(分页数据集, 查询条件,排序条件);  

分页数据集:

[csharp]  view plain  copy
  1. /// <summary>  
  2.     /// 分页获取数据列表  
  3.     /// </summary>  
  4.     public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)  
  5.     {  
  6.         StringBuilder strSql = new StringBuilder();  
  7.         strSql.Append("SELECT * FROM ( ");  
  8.         strSql.Append(" SELECT ROW_NUMBER() OVER (");  
  9.         if (!string.IsNullOrEmpty(orderby.Trim()))  
  10.         {  
  11.             strSql.Append("order by T." + orderby);  
  12.         }  
  13.         else  
  14.         {  
  15.             strSql.Append("order by T.TASK_BEGIN_DATE desc");  
  16.         }  
  17.         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 ");  
  18.         if (!string.IsNullOrEmpty(strWhere.Trim()))  
  19.         {  
  20.             strSql.Append(" WHERE " + strWhere);  
  21.         }  
  22.         strSql.Append(" ) TT");  
  23.         strSql.AppendFormat(" WHERE TT.Rowss between {0} and {1}", startIndex, endIndex);  
  24.         return _DbHelperOra.Query(strSql.ToString());  
  25.     }  

说明:此处只需要指定你自定义的条件就可以了,以上代码展示的只是文章图一的实现

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

好了,到这,我们的所有操作就完成了,来看下我们运行的分页效果图:

 

 DevExpress中如何实现GridControl的分页功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值