1. 程式人生 > >DevExpress中實現GridControl的分頁功能

DevExpress中實現GridControl的分頁功能

cor 只需要 列表 lastindex aso ont ram data span

DevExpress中如何實現GridControl的分頁功能

簡介:DevExpress中如何實現GridControl的分頁功能,

主要是利用DataNavigator和GridControl組合,自定義事件實現分頁功能

接下來,我們就去實現分頁功能,先看下效果圖:

技術分享圖片

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

整個分頁操作,基本分三步:

一:界面層

二:代碼層

三:數據庫

四:調用

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

一:界面層,如圖:

技術分享圖片

說明:放入一個GridControl控件(gridLogList)和DataNavigator控件(nvgtDataPager),給GridControl綁定好列,

設置DataNavigator控件屬性Dock=Bottom;TextLocation=Center;TextStringFormat=第 {0}頁 ,共 {1};

ShowToolTips=true;將下圖中圈中的按鈕屬性visible=False;

如圖:

技術分享圖片

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

二:代碼層

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. nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;
  11. nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
  12. nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
  13. nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
  14. //記錄獲取開始數
  15. int startIndex = (pageIndex - 1) * pagesize + 1;
  16. //結束數
  17. int endIndex = pageIndex * pagesize;
  18. //總行數
  19. int row = objSOL.GetRecordCount(strWhere);
  20. //獲取總頁數
  21. if (row % pagesize > 0)
  22. {
  23. pageCount = row / pagesize + 1;
  24. }
  25. else
  26. {
  27. pageCount = row / pagesize;
  28. }
  29. if (pageIndex == 1)
  30. {
  31. nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
  32. nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; ;
  33. }
  34. //最後頁時獲取真實記錄數
  35. if (pageCount == pageIndex)
  36. {
  37. endIndex = row;
  38. nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
  39. nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
  40. }
  41. //分頁獲取數據列表
  42. DataTable dt = objSOL.GetListByPage(strWhere, "", startIndex, endIndex).Tables[0];
  43. gridLogList.DataSource = dt;
  44. nvgtDataPager.DataSource = dt;
  45. nvgtDataPager.TextStringFormat = string.Format("第 {0}頁, 共 {1}頁", pageIndex, pageCount);
  46. }

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. /// <summary>
  13. /// 分頁事件處理
  14. /// </summary>
  15. /// <param name="eventString">事件名稱</param>
  16. /// <param name="button">按鈕控件</param>
  17. /// <author>PengZhen</author>
  18. /// <time>2013-11-5 14:25:59</time>
  19. void ShowEvent(string eventString, NavigatorButtonBase button)
  20. {
  21. //string type = button.ButtonType.ToString();
  22. NavigatorCustomButton btn = (NavigatorCustomButton)button;
  23. string type = btn.Tag.ToString();
  24. if (type == "首頁")
  25. {
  26. pageIndex = 1;
  27. }
  28. if (type=="下一頁")
  29. {
  30. pageIndex++;
  31. }
  32. if (type=="末頁")
  33. {
  34. pageIndex = pageCount;
  35. }
  36. if (type == "上一頁")
  37. {
  38. pageIndex--;
  39. }
  40. //綁定分頁控件和GridControl數據
  41. BindPageGridList(strWhere);
  42. }

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

三:數據庫

  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. /// <summary>
  4. /// 查詢
  5. /// </summary>
  6. /// <author>PengZhen</author>
  7. /// <time>2013-10-30 11:08:03</time>
  8. /// <param name="sender"></param>
  9. /// <param name="e"></param>
  10. private void btSelect_Click(object sender, EventArgs e)
  11. {
  12. //獲取查詢條件
  13. strWhere = GetSqlWhere();
  14. BindPageGridList(strWhere);
  15. }
  16. /// <summary>
  17. /// 獲取查詢條件
  18. /// </summary>
  19. /// <author>PengZhen</author>
  20. /// <time>2013-11-5 15:25:00</time>
  21. /// <returns>返回查詢條件</returns>
  22. private string GetSqlWhere()
  23. {
  24. //查詢條件
  25. string strReturnWhere = string.Empty;
  26. //用戶編號
  27. string strUserId = string.Empty;
  28. if (!string.IsNullOrEmpty(UserManage.UserID))
  29. {
  30. strUserId = "12";// UserManage.UserID;
  31. }
  32. //分系統編碼
  33. string strSubSystemCode = string.Empty;
  34. if (cbbSubSystemCode.SelectedItem != null)
  35. {
  36. strSubSystemCode = (cbbSubSystemCode.SelectedItem as ComboBoxData).Value;
  37. }
  38. //功能模塊
  39. string strFunctionModule = string.Empty;
  40. if (cbbFunctionModule.SelectedItem != null)
  41. {
  42. strFunctionModule = (cbbFunctionModule.SelectedItem as ComboBoxData).Value;
  43. }
  44. //數據分類
  45. string strDataType = string.Empty;
  46. if (tcbDataType.SelectedNode != null)
  47. {
  48. strDataType = tcbDataType.SelectedNode.Name;
  49. }
  50. //操作類型
  51. string strOperatedType = string.Empty;
  52. if (cbbOperatedType.SelectedItem != null)
  53. {
  54. strOperatedType = (cbbOperatedType.SelectedItem as ComboBoxData).Value;
  55. }
  56. //開始時間
  57. string strStartTime = string.Empty;
  58. if (!string.IsNullOrEmpty(dateStartTime.Text))
  59. {
  60. strStartTime = dateStartTime.Text;
  61. }
  62. //結束時間
  63. string strEndTime = string.Empty;
  64. if (!string.IsNullOrEmpty(dateEndTime.Text))
  65. {
  66. strEndTime = dateEndTime.Text;
  67. }
  68. //用戶ID
  69. if (!string.IsNullOrEmpty(strUserId))
  70. {
  71. strReturnWhere += "USER_ID=‘" + strUserId + "‘ and";
  72. }
  73. //分系統代碼
  74. if (!string.IsNullOrEmpty(strSubSystemCode))
  75. {
  76. strReturnWhere += "SYSTEM_CODE=‘" + strSubSystemCode + "‘ and";
  77. }
  78. //模塊編號
  79. if (!string.IsNullOrEmpty(strFunctionModule))
  80. {
  81. strReturnWhere += "MODULE_ID=‘" + strFunctionModule + "‘ and";
  82. }
  83. //數據分類代碼
  84. if (!string.IsNullOrEmpty(strDataType))
  85. {
  86. strReturnWhere += "DATA_CATAGORY_CODE=‘" + strDataType + "‘ and";
  87. }
  88. //操作類型
  89. if (!string.IsNullOrEmpty(strOperatedType))
  90. {
  91. strReturnWhere += "OPERATE_TYPE=‘" + strOperatedType + "‘ and";
  92. }
  93. //操作時間
  94. if (!string.IsNullOrEmpty(strStartTime) && !string.IsNullOrEmpty(strEndTime))
  95. {
  96. strReturnWhere += "OPERATE_DATE between ‘" + strStartTime + "‘ and ‘" + strEndTime + "‘";
  97. }
  98. if (!string.IsNullOrEmpty(strReturnWhere))
  99. {
  100. strReturnWhere = strReturnWhere.Remove(strReturnWhere.LastIndexOf("and"));
  101. }
  102. return strReturnWhere;
  103. }

說明:此處只需要指定你自定義的條件就可以了,以上代碼展示的只是文章圖一的實現

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

好了,到這,我們的所有操作就完成了,來看下我們運行的分頁效果圖:

技術分享圖片

DevExpress中如何實現GridControl的分頁功能(組件)

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

DevExpress中實現GridControl的分頁功能