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

简介:DevExpress中如何实现GridControl的分页功能,

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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

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

DevExpress中实现GridControl的分页功能 随笔 第1张

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

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

一:界面层

二:代码层

三:数据库

四:调用

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

一:界面层,如图:

 DevExpress中实现GridControl的分页功能 随笔 第2张

说明:放入一个GridControl控件(gridLogList)和DataNavigator控件(nvgtDataPager),给GridControl绑定好列,

        设置DataNavigator控件属性Dock=Bottom;TextLocation=Center;TextStringFormat=第 {0}页 ,共 {1};

        ShowToolTips=true;将下图中圈中的按钮属性visible=False;

如图:

DevExpress中实现GridControl的分页功能 随笔 第3张

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

二:代码层

1.定义变量

  1.   //页行数
  2.   public int pagesize = 20;
  3.   //当前页
  4.   public int pageIndex = 1;
  5.   //总页数
  6.   public int pageCount;

2.定义方法

  1.   /// <summary>
  2.   /// 绑定分页控件和GridControl数据
  3.   /// </summary>
  4.   /// <author>PengZhen</author>
  5.   /// <time>2013-11-5 14:22:22</time>
  6.   /// <param name="strWhere">查询条件</param>
  7.   public void BindPageGridList(string strWhere)
  8.   {
  9.   SystemOperateLog objSOL = new BLL.SystemOperateLog();
  10.    
  11.   nvgtDataPager.Buttons.CustomButtons[ 0].Enabled = true;
  12.   nvgtDataPager.Buttons.CustomButtons[ 1].Enabled = true;
  13.   nvgtDataPager.Buttons.CustomButtons[ 2].Enabled = true;
  14.   nvgtDataPager.Buttons.CustomButtons[ 3].Enabled = true;
  15.   //记录获取开始数
  16.   int startIndex = (pageIndex - 1) * pagesize + 1;
  17.   //结束数
  18.   int endIndex = pageIndex * pagesize;
  19.    
  20.   //总行数
  21.   int row = objSOL.GetRecordCount(strWhere);
  22.    
  23.   //获取总页数
  24.   if (row % pagesize > 0)
  25.   {
  26.   pageCount = row / pagesize + 1;
  27.   }
  28.   else
  29.   {
  30.   pageCount = row / pagesize;
  31.   }
  32.    
  33.   if (pageIndex == 1)
  34.   {
  35.   nvgtDataPager.Buttons.CustomButtons[ 0].Enabled = false;
  36.   nvgtDataPager.Buttons.CustomButtons[ 1].Enabled = false; ;
  37.   }
  38.    
  39.   //最后页时获取真实记录数
  40.   if (pageCount == pageIndex)
  41.   {
  42.   endIndex = row;
  43.   nvgtDataPager.Buttons.CustomButtons[ 2].Enabled = false;
  44.   nvgtDataPager.Buttons.CustomButtons[ 3].Enabled = false;
  45.   }
  46.    
  47.   //分页获取数据列表
  48.   DataTable dt = objSOL.GetListByPage(strWhere, "", startIndex, endIndex).Tables[ 0];
  49.    
  50.   gridLogList.DataSource = dt;
  51.    
  52.   nvgtDataPager.DataSource = dt;
  53.   nvgtDataPager.TextStringFormat = string.Format( "第 {0}页, 共 {1}页", pageIndex, pageCount);
  54.   }

3.定义事件

  1.   /// <summary>
  2.   /// 按钮点击事件
  3.   /// </summary>
  4.   /// <author>PengZhen</author>
  5.   /// <time>2013-11-5 14:24:25</time>
  6.   /// <param name="sender"></param>
  7.   /// <param name="e"></param>
  8.   private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
  9.   {
  10.   ShowEvent( "ButtonClick", e.Button);
  11.   }
  12.    
  13.   /// <summary>
  14.   /// 分页事件处理
  15.   /// </summary>
  16.   /// <param name="eventString">事件名称</param>
  17.   /// <param name="button">按钮控件</param>
  18.   /// <author>PengZhen</author>
  19.   /// <time>2013-11-5 14:25:59</time>
  20.   void ShowEvent(string eventString, NavigatorButtonBase button)
  21.   {
  22.   //string type = button.ButtonType.ToString();
  23.   NavigatorCustomButton btn = (NavigatorCustomButton)button;
  24.   string type = btn.Tag.ToString();
  25.   if (type == "首页")
  26.   {
  27.   pageIndex = 1;
  28.   }
  29.    
  30.   if (type== "下一页")
  31.   {
  32.   pageIndex++;
  33.   }
  34.    
  35.   if (type== "末页")
  36.   {
  37.   pageIndex = pageCount;
  38.   }
  39.    
  40.   if (type == "上一页")
  41.   {
  42.   pageIndex--;
  43.   }
  44.    
  45.   //绑定分页控件和GridControl数据
  46.   BindPageGridList(strWhere);
  47.   }

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

三:数据库

  1.   /// <summary>
  2.   /// 获取记录总数
  3.   /// </summary>
  4.   public int GetRecordCount(string strWhere)
  5.   {
  6.   StringBuilder strSql = new StringBuilder();
  7.   strSql.Append( "select count(1) FROM TL_SYSTEM_OPERATE_LOGS ");
  8.   if (strWhere.Trim() != "")
  9.   {
  10.   strSql.Append( " where " + strWhere);
  11.   }
  12.   object obj = _DbHelperOra.GetSingle(strSql.ToString());
  13.   if (obj == null)
  14.   {
  15.   return 0;
  16.   }
  17.   else
  18.   {
  19.   return Convert.ToInt32(obj);
  20.   }
  21.   }
  22.   /// <summary>
  23.   /// 分页获取数据列表
  24.   /// </summary>
  25.   public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
  26.   {
  27.   StringBuilder strSql = new StringBuilder();
  28.   strSql.Append( "SELECT * FROM ( ");
  29.   strSql.Append( " SELECT ROW_NUMBER() OVER (");
  30.   if (! string.IsNullOrEmpty( orderby.Trim()))
  31.   {
  32.   strSql.Append( "order by T." + orderby);
  33.   }
  34.   else
  35.   {
  36.   strSql.Append( "order by T.ID desc");
  37.   }
  38.   strSql.Append( ")AS Rowssss, T.* from TL_SYSTEM_OPERATE_LOGS T ");
  39.   if (! string.IsNullOrEmpty(strWhere.Trim()))
  40.   {
  41.   strSql.Append( " WHERE " + strWhere);
  42.   }
  43.   strSql.Append( " ) TT");
  44.   strSql.AppendFormat( " WHERE TT.Rowssss between {0} and {1}", startIndex, endIndex);
  45.   return _DbHelperOra.Query(strSql.ToString());
  46.   }

说明:数据库的操作只作为借鉴,请根据自己的表做相应的修改

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

四:调用

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

  1.   //查询条件
  2.   private static string strWhere = string.Empty;
  3.    
  4.   /// <summary>
  5.   /// 查询
  6.   /// </summary>
  7.   /// <author>PengZhen</author>
  8.   /// <time>2013-10-30 11:08:03</time>
  9.   /// <param name="sender"></param>
  10.   /// <param name="e"></param>
  11.   private void btSelect_Click(object sender, EventArgs e)
  12.   {
  13.   //获取查询条件
  14.   strWhere = GetSqlWhere();
  15.    
  16.   BindPageGridList(strWhere);
  17.   }
  18.    
  19.   /// <summary>
  20.   /// 获取查询条件
  21.   /// </summary>
  22.   /// <author>PengZhen</author>
  23.   /// <time>2013-11-5 15:25:00</time>
  24.   /// <returns>返回查询条件</returns>
  25.   private string GetSqlWhere()
  26.   {
  27.   //查询条件
  28.   string strReturnWhere = string.Empty;
  29.    
  30.   //用户编号
  31.   string strUserId = string.Empty;
  32.    
  33.   if (! string.IsNullOrEmpty(UserManage.UserID))
  34.   {
  35.   strUserId = "12"; // UserManage.UserID;
  36.   }
  37.   //分系统编码
  38.   string strSubSystemCode = string.Empty;
  39.    
  40.   if (cbbSubSystemCode.SelectedItem != null)
  41.   {
  42.   strSubSystemCode = (cbbSubSystemCode.SelectedItem as ComboBoxData).Value;
  43.   }
  44.    
  45.   //功能模块
  46.   string strFunctionModule = string.Empty;
  47.    
  48.   if (cbbFunctionModule.SelectedItem != null)
  49.   {
  50.   strFunctionModule = (cbbFunctionModule.SelectedItem as ComboBoxData).Value;
  51.   }
  52.    
  53.   //数据分类
  54.   string strDataType = string.Empty;
  55.    
  56.   if (tcbDataType.SelectedNode != null)
  57.   {
  58.   strDataType = tcbDataType.SelectedNode.Name;
  59.   }
  60.    
  61.   //操作类型
  62.   string strOperatedType = string.Empty;
  63.    
  64.   if (cbbOperatedType.SelectedItem != null)
  65.   {
  66.   strOperatedType = (cbbOperatedType.SelectedItem as ComboBoxData).Value;
  67.   }
  68.   //开始时间
  69.   string strStartTime = string.Empty;
  70.    
  71.   if (! string.IsNullOrEmpty(dateStartTime.Text))
  72.   {
  73.   strStartTime = dateStartTime.Text;
  74.   }
  75.   //结束时间
  76.   string strEndTime = string.Empty;
  77.    
  78.   if (! string.IsNullOrEmpty(dateEndTime.Text))
  79.   {
  80.   strEndTime = dateEndTime.Text;
  81.   }
  82.    
  83.   //用户ID
  84.   if (! string.IsNullOrEmpty(strUserId))
  85.   {
  86.   strReturnWhere += "USER_ID='" + strUserId + "' and";
  87.   }
  88.   //分系统代码
  89.   if (! string.IsNullOrEmpty(strSubSystemCode))
  90.   {
  91.   strReturnWhere += "SYSTEM_CODE='" + strSubSystemCode + "' and";
  92.   }
  93.   //模块编号
  94.   if (! string.IsNullOrEmpty(strFunctionModule))
  95.   {
  96.   strReturnWhere += "MODULE_ID='" + strFunctionModule + "' and";
  97.   }
  98.   //数据分类代码
  99.   if (! string.IsNullOrEmpty(strDataType))
  100.   {
  101.   strReturnWhere += "DATA_CATAGORY_CODE='" + strDataType + "' and";
  102.   }
  103.   //操作类型
  104.   if (! string.IsNullOrEmpty(strOperatedType))
  105.   {
  106.   strReturnWhere += "OPERATE_TYPE='" + strOperatedType + "' and";
  107.   }
  108.   //操作时间
  109.   if (! string.IsNullOrEmpty(strStartTime) && ! string.IsNullOrEmpty(strEndTime))
  110.   {
  111.   strReturnWhere += "OPERATE_DATE between '" + strStartTime + "' and '" + strEndTime + "'";
  112.   }
  113.    
  114.   if (! string.IsNullOrEmpty(strReturnWhere))
  115.   {
  116.   strReturnWhere = strReturnWhere.Remove(strReturnWhere.LastIndexOf( "and"));
  117.   }
  118.    
  119.   return strReturnWhere;
  120.   }

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

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

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

DevExpress中实现GridControl的分页功能 随笔 第4张

 

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

 

 

出处: https://blog.csdn.net/pengzhen8805/article/details/14169327

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄