#region using System.Collections; using System.Data; using System.Drawing; using System.Windows.Forms; using CSFrameworkV5.Common; using CSFrameworkV5.Core; using CSFrameworkV5.Interfaces; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid; #endregion namespace CSFrameworkV5.Library.CommonClass { /// /// 分页查询通用类(缓存分页数据) /// public class QueryByPageCache : IMyPageDataCache { private ISupportPageSearch _BusinessBLL; //业务逻辑层接口 private int _CurrentPageNo = 1; //当前页号 private GridView _DataView; //数据视图,如GridControl private bool _IsChcheData; private int _PageCount; //本次搜索结果总页数 private Hashtable _PageDataMapping; //哈希表,存储每页的数据 private int _PageSize = 30; //预设每页返回30条记录 private object[] _SearchConditions; //本次查询的查询条件 /// /// 构造器 /// /// GridView表格 /// 支持分页查询的BLL层 /// 每页记录数 /// 是否缓存每页的数据(缺点:数据会过时,变成脏数据) public QueryByPageCache(GridView dataView, ISupportPageSearch BLL, int defaultPageSize, bool cacheData = false) { _PageSize = defaultPageSize; _PageDataMapping = new Hashtable(); _DataView = dataView; _BusinessBLL = BLL; _IsChcheData = cacheData; _DataView.GridControl.EmbeddedNavigator.ButtonClick += navigator_ButtonClick; AddNavigatorPageButton(_DataView.GridControl.EmbeddedNavigator); SetPageButtonEnable(false); } /// /// 添加分页按钮 /// /// private void AddNavigatorPageButton(ControlNavigator navigator) { var img = new ImageList(); img.ImageSize = new Size(40, 16); img.Images.Add(Globals.LoadImage("NAV_FIRST_PAGE.ico")); img.Images.Add(Globals.LoadImage("NAV_PRIOR_PAGE.ico")); img.Images.Add(Globals.LoadImage("NAV_NEXT_PAGE.ico")); img.Images.Add(Globals.LoadImage("NAV_LAST_PAGE.ico")); navigator.Buttons.ImageList = img; navigator.CustomButtons.Clear(); var btnMoveFirst = new NavigatorCustomButton(0, "最前一页"); var btnMovePrior = new NavigatorCustomButton(1, "上一页"); var btnMoveNext1 = new NavigatorCustomButton(2, "下一页"); var btnMoveLast1 = new NavigatorCustomButton(3, "最后一页"); btnMoveFirst.Tag = "FIRST_PAGE"; btnMovePrior.Tag = "PRIOR_PAGE"; btnMoveNext1.Tag = "NEXT_PAGE"; btnMoveLast1.Tag = "LAST_PAGE"; navigator.CustomButtons.AddRange(new[] { btnMoveFirst, btnMovePrior, btnMoveNext1, btnMoveLast1 }); } #region 导航按钮及事件 public void SetPageButtonEnable(bool enable) { var navigator = _DataView.GridControl.EmbeddedNavigator; foreach (NavigatorCustomButton btn in navigator.Buttons .CustomButtons) btn.Enabled = enable; } private void navigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e) { if (_SearchConditions == null) { Msg.Warning("搜索器没有初始化,请先输入查询条件并查询数据!"); return; } if (e.Button.Tag != null) { var tag = e.Button.Tag.ToStringEx().ToUpper(); if (tag == "FIRST_PAGE") GotoFirstPage(); //第一页 if (tag == "PRIOR_PAGE") GotoPriorPage(); //上一页 if (tag == "NEXT_PAGE") GotoNextPage(); //下一页 if (tag == "LAST_PAGE") GotoLastPage(); //最后页 } } private void GotoNextPage() { _CurrentPageNo++; if (_CurrentPageNo > _PageCount) { _CurrentPageNo--; return; } var data = GetPageData(_CurrentPageNo); _DataView.GridControl.DataSource = data; } private void GotoPriorPage() { _CurrentPageNo--; if (_CurrentPageNo <= 0) { _CurrentPageNo++; return; } var data = GetPageData(_CurrentPageNo); _DataView.GridControl.DataSource = data; } private void GotoFirstPage() { _CurrentPageNo = 1; var data = GetPageData(_CurrentPageNo); _DataView.GridControl.DataSource = data; } private void GotoLastPage() { _CurrentPageNo = _PageCount; var data = GetPageData(_CurrentPageNo); _DataView.GridControl.DataSource = data; } #endregion #region IPageDataCache Members public int PageSize { get => _PageSize; set => _PageSize = value; } public int PageCount => _PageCount; public int CurrentPage => _CurrentPageNo; public int CachedPage => _PageDataMapping.Keys.Count; /// /// 获取指定页码的数据 /// /// /// public DataTable GetPageData(int pageNo) { if (pageNo <= 0) pageNo = 1; //不缓存每页数据,清空缓存器,程序会从数据库下载 if (_IsChcheData == false) _PageDataMapping.Clear(); //有缓存数据 if (_PageDataMapping.ContainsKey(pageNo)) return _PageDataMapping[pageNo] as DataTable; //从缓存表撷取数据 try { Cursor.Current = Cursors.WaitCursor; //无缓存数据,从数据库下载然后加入缓存表 var thisPageData = _BusinessBLL.QueryByPage(_SearchConditions, pageNo, _PageSize); //下载数据 if (thisPageData != null && thisPageData.Rows.Count > 0) _PageDataMapping.Add(pageNo, thisPageData); //加入缓存表 return thisPageData; //返回数据 } finally { Cursor.Current = Cursors.Default; } } /// /// 初始化缓存数据管理器 /// /// 查询条件 public void InitializeSearch(object[] searchConditions) { _PageDataMapping.Clear(); //清空哈希表 _SearchConditions = searchConditions; var firstPageData = _BusinessBLL.QueryByPage(searchConditions, 1, _PageSize); if (_BusinessBLL.TotalPages > 0) { _CurrentPageNo = 1; _PageDataMapping.Add(1, firstPageData); //下载第一页数据,加入缓存表 _PageCount = _BusinessBLL .TotalPages; //取总页数 } _DataView.GridControl.DataSource = firstPageData; } public void ClearCache() { _CurrentPageNo = 0; _PageCount = 0; _PageDataMapping.Clear(); } #endregion } }