DevExpress中實現GridControl的分頁功能

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

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

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

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

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------spa

整個分頁操做,基本分三步:.net

一:界面層3d

二:代碼層code

三:數據庫orm

四:調用htm

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

一:界面層,如圖:

 

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

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

        ShowToolTips=true;

在DataNavigator控件屬性Buttons --> CustomButtons  (集合)  在右側點擊按鈕,打開自定義按鈕對話框,以下圖:

點擊Add按鈕,添加4個自定義按鈕,分別爲每個按鈕設置:

ImageIndex屬性:設置顯示的樣式,本身根據功能設按鈕樣式

Tag屬性:用於判斷點擊的按鈕,四個按鈕分別設置:首頁:f;上一頁:p;下一頁:n;最後一頁:l

將下圖中圈中的按鈕屬性visible=False;  如圖:

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

最主要的設置DataNavigator控件的ButtonClick事件,下面是我本身在項目中用到的,做爲參考,

        private void dataNavigator1_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            PageInfo pageInfo = dataNavigator1.Tag as PageInfo;
            if (pageInfo != null)
            {
                //首頁
                if ((e.Button).ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Custom && e.Button.Tag.ToString() == "f")
                {
                    pageInfo.CurrentPage = 1;
                }

                //上一頁
                if ((e.Button).ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Custom && e.Button.Tag.ToString() == "p")
                {

                    if (pageInfo.HasPrevious == true)
                        pageInfo.CurrentPage--;
                }

                //下一頁
                if ((e.Button).ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Custom && e.Button.Tag.ToString() == "n")
                {
                    if (pageInfo.HasNext == true)
                        pageInfo.CurrentPage++;

                }

                //最後一頁
                if ((e.Button).ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Custom && e.Button.Tag.ToString() == "l")
                {
                    pageInfo.CurrentPage = pageInfo.TotalPages;
                }
            }
            QueryCriteria();//按條件執行查詢
        }

 

也 能夠參考下面的代碼

二:代碼層

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的分頁功能(組件)

 

 

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

相關文章
相關標籤/搜索