DevExpress中實現GridControl的分頁功能
DevExpress中如何實現GridControl的分頁功能
簡介:DevExpress中如何實現GridControl的分頁功能,
主要是利用DataNavigator和GridControl組合,自定義事件實現分頁功能
接下來,我們就去實現分頁功能,先看下效果圖:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
整個分頁操作,基本分三步:
一:界面層
二:代碼層
三:數據庫
四:調用
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
一:界面層,如圖:
說明:放入一個GridControl控件(gridLogList)和DataNavigator控件(nvgtDataPager),給GridControl綁定好列,
設置DataNavigator控件屬性Dock=Bottom;TextLocation=Center;TextStringFormat=第 {0}頁 ,共 {1};
ShowToolTips=true;將下圖中圈中的按鈕屬性visible=False;
如圖:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二:代碼層
1.定義變量
- //頁行數
- public int pagesize = 20;
- //當前頁
- public int pageIndex = 1;
- //總頁數
- public int pageCount;
2.定義方法
- /// <summary>
- /// 綁定分頁控件和GridControl數據
- /// </summary>
- /// <author>PengZhen</author>
- /// <time>2013-11-5 14:22:22</time>
- /// <param name="strWhere">查詢條件</param>
- public void BindPageGridList(string strWhere)
- {
- SystemOperateLog objSOL = new BLL.SystemOperateLog();
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
- //記錄獲取開始數
- int startIndex = (pageIndex - 1) * pagesize + 1;
- //結束數
- int endIndex = pageIndex * pagesize;
- //總行數
- int row = objSOL.GetRecordCount(strWhere);
- //獲取總頁數
- if (row % pagesize > 0)
- {
- pageCount = row / pagesize + 1;
- }
- else
- {
- pageCount = row / pagesize;
- }
- if (pageIndex == 1)
- {
- nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; ;
- }
- //最後頁時獲取真實記錄數
- if (pageCount == pageIndex)
- {
- endIndex = row;
- nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
- nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
- }
- //分頁獲取數據列表
- DataTable dt = objSOL.GetListByPage(strWhere, "", startIndex, endIndex).Tables[0];
- gridLogList.DataSource = dt;
- nvgtDataPager.DataSource = dt;
- nvgtDataPager.TextStringFormat = string.Format("第 {0}頁, 共 {1}頁", pageIndex, pageCount);
- }
3.定義事件
- /// <summary>
- /// 按鈕點擊事件
- /// </summary>
- /// <author>PengZhen</author>
- /// <time>2013-11-5 14:24:25</time>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
- {
- ShowEvent("ButtonClick", e.Button);
- }
- /// <summary>
- /// 分頁事件處理
- /// </summary>
- /// <param name="eventString">事件名稱</param>
- /// <param name="button">按鈕控件</param>
- /// <author>PengZhen</author>
- /// <time>2013-11-5 14:25:59</time>
- 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--;
- }
- //綁定分頁控件和GridControl數據
- BindPageGridList(strWhere);
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三:數據庫
- /// <summary>
- /// 獲取記錄總數
- /// </summary>
- public int GetRecordCount(string strWhere)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select count(1) FROM TL_SYSTEM_OPERATE_LOGS ");
- if (strWhere.Trim() != "")
- {
- strSql.Append(" where " + strWhere);
- }
- object obj = _DbHelperOra.GetSingle(strSql.ToString());
- if (obj == null)
- {
- return 0;
- }
- else
- {
- return Convert.ToInt32(obj);
- }
- }
- /// <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.ID desc");
- }
- strSql.Append(")AS Rowssss, T.* from TL_SYSTEM_OPERATE_LOGS T ");
- if (!string.IsNullOrEmpty(strWhere.Trim()))
- {
- strSql.Append(" WHERE " + strWhere);
- }
- strSql.Append(" ) TT");
- strSql.AppendFormat(" WHERE TT.Rowssss between {0} and {1}", startIndex, endIndex);
- return _DbHelperOra.Query(strSql.ToString());
- }
說明:數據庫的操作只作為借鑒,請根據自己的表做相應的修改
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
四:調用
如文章開頭第一張效果圖所述,當我就行查詢操作時,也就是調用分頁和綁定數據,需要做的操作,如下:
- //查詢條件
- private static string strWhere = string.Empty;
- /// <summary>
- /// 查詢
- /// </summary>
- /// <author>PengZhen</author>
- /// <time>2013-10-30 11:08:03</time>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btSelect_Click(object sender, EventArgs e)
- {
- //獲取查詢條件
- strWhere = GetSqlWhere();
- BindPageGridList(strWhere);
- }
- /// <summary>
- /// 獲取查詢條件
- /// </summary>
- /// <author>PengZhen</author>
- /// <time>2013-11-5 15:25:00</time>
- /// <returns>返回查詢條件</returns>
- private string GetSqlWhere()
- {
- //查詢條件
- string strReturnWhere = string.Empty;
- //用戶編號
- string strUserId = string.Empty;
- if (!string.IsNullOrEmpty(UserManage.UserID))
- {
- strUserId = "12";// UserManage.UserID;
- }
- //分系統編碼
- string strSubSystemCode = string.Empty;
- if (cbbSubSystemCode.SelectedItem != null)
- {
- strSubSystemCode = (cbbSubSystemCode.SelectedItem as ComboBoxData).Value;
- }
- //功能模塊
- string strFunctionModule = string.Empty;
- if (cbbFunctionModule.SelectedItem != null)
- {
- strFunctionModule = (cbbFunctionModule.SelectedItem as ComboBoxData).Value;
- }
- //數據分類
- string strDataType = string.Empty;
- if (tcbDataType.SelectedNode != null)
- {
- strDataType = tcbDataType.SelectedNode.Name;
- }
- //操作類型
- string strOperatedType = string.Empty;
- if (cbbOperatedType.SelectedItem != null)
- {
- strOperatedType = (cbbOperatedType.SelectedItem as ComboBoxData).Value;
- }
- //開始時間
- string strStartTime = string.Empty;
- if (!string.IsNullOrEmpty(dateStartTime.Text))
- {
- strStartTime = dateStartTime.Text;
- }
- //結束時間
- string strEndTime = string.Empty;
- if (!string.IsNullOrEmpty(dateEndTime.Text))
- {
- strEndTime = dateEndTime.Text;
- }
- //用戶ID
- if (!string.IsNullOrEmpty(strUserId))
- {
- strReturnWhere += "USER_ID=‘" + strUserId + "‘ and";
- }
- //分系統代碼
- if (!string.IsNullOrEmpty(strSubSystemCode))
- {
- strReturnWhere += "SYSTEM_CODE=‘" + strSubSystemCode + "‘ and";
- }
- //模塊編號
- if (!string.IsNullOrEmpty(strFunctionModule))
- {
- strReturnWhere += "MODULE_ID=‘" + strFunctionModule + "‘ and";
- }
- //數據分類代碼
- if (!string.IsNullOrEmpty(strDataType))
- {
- strReturnWhere += "DATA_CATAGORY_CODE=‘" + strDataType + "‘ and";
- }
- //操作類型
- if (!string.IsNullOrEmpty(strOperatedType))
- {
- strReturnWhere += "OPERATE_TYPE=‘" + strOperatedType + "‘ and";
- }
- //操作時間
- if (!string.IsNullOrEmpty(strStartTime) && !string.IsNullOrEmpty(strEndTime))
- {
- strReturnWhere += "OPERATE_DATE between ‘" + strStartTime + "‘ and ‘" + strEndTime + "‘";
- }
- if (!string.IsNullOrEmpty(strReturnWhere))
- {
- strReturnWhere = strReturnWhere.Remove(strReturnWhere.LastIndexOf("and"));
- }
- return strReturnWhere;
- }
說明:此處只需要指定你自定義的條件就可以了,以上代碼展示的只是文章圖一的實現
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
好了,到這,我們的所有操作就完成了,來看下我們運行的分頁效果圖:
DevExpress中如何實現GridControl的分頁功能(組件)
出處: https://blog.csdn.net/pengzhen8805/article/details/14169327
DevExpress中實現GridControl的分頁功能