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;
-
-
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的分页功能(组件)