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.定義變量
-
-
public
int pagesize =
20;
-
-
public
int pageIndex =
1;
-
-
2.定義方法
-
-
-
-
-
-
-
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);
-
-
-
-
-
pageCount = row / pagesize +
1;
-
-
-
-
pageCount = row / pagesize;
-
-
-
-
-
nvgtDataPager.Buttons.CustomButtons[
0].Enabled =
false;
-
nvgtDataPager.Buttons.CustomButtons[
1].Enabled =
false; ;
-
-
-
-
if (pageCount == pageIndex)
-
-
-
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.定義事件
-
-
-
-
-
-
-
-
private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
-
-
ShowEvent(
"ButtonClick", e.Button);
-
-
-
-
-
-
-
-
-
-
void ShowEvent(string eventString, NavigatorButtonBase button)
-
-
-
NavigatorCustomButton btn = (NavigatorCustomButton)button;
-
string type = btn.Tag.ToString();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
BindPageGridList(strWhere);
-
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三:數據庫
-
-
-
-
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());
-
-
-
-
-
-
-
return Convert.ToInt32(obj);
-
-
-
-
-
-
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);
-
-
-
-
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.AppendFormat(
" WHERE TT.Rowssss between {0} and {1}", startIndex, endIndex);
-
return _DbHelperOra.Query(strSql.ToString());
-
說明:數據庫的操做只做爲借鑑,請根據本身的表作相應的修改
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
四:調用
如文章開頭第一張效果圖所述,當我就行查詢操做時,也就是調用分頁和綁定數據,須要作的操做,以下:
-
-
private
static
string strWhere =
string.Empty;
-
-
-
-
-
-
-
-
-
private void btSelect_Click(object sender, EventArgs e)
-
-
-
strWhere = GetSqlWhere();
-
-
BindPageGridList(strWhere);
-
-
-
-
-
-
-
-
-
private string GetSqlWhere()
-
-
-
string strReturnWhere =
string.Empty;
-
-
-
string strUserId =
string.Empty;
-
-
if (!
string.IsNullOrEmpty(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;
-
-
-
-
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"));
-
-
-
-
說明:此處只須要指定你自定義的條件就能夠了,以上代碼展現的只是文章圖一的實現
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
好了,到這,咱們的全部操做就完成了,來看下咱們運行的分頁效果圖:
DevExpress中如何實現GridControl的分頁功能(組件)