首页 > 代码库 > [原创]WinForm分页控件制作

[原创]WinForm分页控件制作

先简单说一下思路:

1、做一个分页控件的导航类,即记录总页数、当前页、每页记录数,下一页、上一页、跳转等操作的页数变更。

class PageNavigation
{
/// <summary>
/// display item count for per page
/// default value is 20
/// </summary>
private int _iPerItemCount = 20;
public int PerItemCount
{
get { return _iPerItemCount; }
set { _iPerItemCount = value; }
}

/// <summary>
/// total item count
/// </summary>
private int _iTotalItemCount;
public int TotalItemCount
{
get { return _iTotalItemCount; }
set { _iTotalItemCount = value; }
}

/// <summary>
/// current page index
/// </summary>
private int _iCurrentPageIndex = 1;
public int CurrentPageIndex
{
get { return _iCurrentPageIndex; }
set
{
if (value > PageCount)
{
_iCurrentPageIndex = PageCount;
return;
}
if (value < 1)
{
_iCurrentPageIndex = 1;
return;
}
_iCurrentPageIndex = value;

}
}

/// <summary>
/// total page count
/// </summary>
public int PageCount
{
get
{
if (_iTotalItemCount != 0 && _iPerItemCount != 0)
return (int)Math.Ceiling(((decimal)_iTotalItemCount) / ((decimal)_iPerItemCount));
else return 1;
}
}

/// <summary>
/// current start item index for current page index
/// </summary>
public int CurrentStartItemIndex
{
get { return (CurrentPageIndex - 1) * _iPerItemCount + 1; }
}

/// <summary>
/// current end item index for current page index
/// </summary>
public int CurrentEndItemIndex
{
get
{
return CurrentPageIndex * _iPerItemCount > _iTotalItemCount ? _iTotalItemCount :
CurrentPageIndex * _iPerItemCount;
}
}

public int CurrentItemCount
{
get { return CurrentEndItemIndex - CurrentStartItemIndex <= 0 ? 0 : CurrentEndItemIndex - CurrentStartItemIndex + 1; }
}

/// <summary>
/// jump to next page
/// </summary>
public void JumpToNextPage()
{
CurrentPageIndex += 1;
}

/// <summary>
/// jump to pre page
/// </summary>
public void JumpToPrePage()
{
CurrentPageIndex = _iCurrentPageIndex == 1 ? _iCurrentPageIndex : _iCurrentPageIndex - 1;
}

/// <summary>
/// jump to first page
/// </summary>
public void JumpToFirstPage()
{
CurrentPageIndex = 1;
}

/// <summary>
/// jump to last page
/// </summary>
public void JumpToLastPage()
{
CurrentPageIndex = PageCount;
}

/// <summary>
/// jump to user page
/// </summary>
/// <param name="iCurPageIndex"></param>
public void Jump(int iCurPageIndex)
{
CurrentPageIndex = iCurPageIndex;
}
}

2、导航控件(UserControl)制作,即将导航类与导航页联系到一起

注:此控件与传统的控件不同之处:将宿主通过属性传递给了导航控件。这样做的好处:宿主不用直接处理数据源的绑定工作,外部调用的时候直接将宿主传进导航控件即可,绑定数据源直接操作导航控件数据源。这样做的缺点:违背了面向对象单一原则。(但是控件都是为了业务服务的嘛,这样用起来比较方便,只要给导航个宿主和数据源,它就可以自己自动实现分页操作了)。

public partial class CtrlPageNavigation : UserControl
{
private PageNavigation _pageNavigation;

public int PerItemCount
{
get { return _pageNavigation.PerItemCount; }
set { _pageNavigation.PerItemCount = value; }
}

public int TotalItemCount
{
get { return _pageNavigation.TotalItemCount; }
set { _pageNavigation.TotalItemCount = value; }
}

private GridControl _hostGridControl; 
public GridControl HostGridControl  // 宿主
{
get { return _hostGridControl; }
set
{
_hostGridControl = value;
if (_hostGridControl != null)
{
GridView view = _hostGridControl.DefaultView as GridView;
view.CustomDrawRowIndicator +=new RowIndicatorCustomDrawEventHandler(view_CustomDrawRowIndicator);  // 用于显示行号
}
}
}

private IList _dataSource;
public IList DataSource
{
get { return _dataSource; }
set
{
_dataSource = value;
_pageNavigation.CurrentPageIndex = 1;
RefreshHostGridControl();
}
}

public CtrlPageNavigation()
{
InitializeComponent();

_pageNavigation = new PageNavigation();
_pageNavigation.PerItemCount = 20; // 设置默认值

this.ParentChanged += new EventHandler(OnParentChanged);  // 找到父类,设置窗体快捷键
}

public void RefreshData()
{
RefreshHostGridControl();
RefreshCurrentPage();
}

private void RefreshHostGridControl()
{
if (this._hostGridControl == null) return;

List<object> currentDataSource = new List<object>();

if (_dataSource != null && _dataSource.Count != 0)
{
for (int i = _pageNavigation.CurrentStartItemIndex - 1; i < _pageNavigation.CurrentEndItemIndex; i++)
{
currentDataSource.Add(_dataSource[i]);
}
}

this._hostGridControl.DataSource = currentDataSource;
this._hostGridControl.RefreshDataSource();
}

private void RefreshCurrentPage()
{
this.txtPageIndex.Value = http://www.mamicode.com/_pageNavigation.CurrentPageIndex;

this.lblTotal.Text =
string.Format(
"总计{0}条,当前{1}/{2}页",
this._pageNavigation.TotalItemCount,
this._pageNavigation.CurrentPageIndex,
this._pageNavigation.PageCount
);
}

private void OnFirstPageClick(object sender, EventArgs e)
{
_pageNavigation.JumpToFirstPage();
RefreshData();

if (FirstPageClick != null)
FirstPageClick(sender, e);
}

private void OnPrePageClick(object sender, EventArgs e)
{
_pageNavigation.JumpToPrePage();
RefreshData();

if (PrePageClick != null)
PrePageClick(sender, e);
}

private void OnNextPageClick(object sender, EventArgs e)
{
_pageNavigation.JumpToNextPage();
RefreshData();

if (NextPageClick != null)
NextPageClick(sender, e);
}

private void OnLastPageClick(object sender, EventArgs e)
{
_pageNavigation.JumpToLastPage();
RefreshData();

if (LastPageClick != null)
LastPageClick(sender, e);
}

private void OnJumpClick(object sender, EventArgs e)
{
_pageNavigation.Jump((int)this.txtPageIndex.Value);
RefreshData();

if (JumpClick != null)
JumpClick(sender, e);
}

// 显示行号,不需要的朋友可以不用的哦

void view_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + this._pageNavigation.CurrentStartItemIndex).ToString().Trim();
}
}

// 这里主要是注册快捷键操作,不需要的朋友可以不用的哦

void OnParentChanged(object sender, EventArgs e)
{
if (this.ParentForm != null)
{
// 注册快捷键
this.ParentForm.KeyPreview = true;
this.ParentForm.KeyUp += new KeyEventHandler(OnParentFormKeyUp);
}
}

void OnParentFormKeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.PageUp:
this.btnPrePage.PerformClick();
break;
case Keys.PageDown:
this.btnNextPage.PerformClick();
break;
}
}

public event EventHandler FirstPageClick;  // 对外提供的事件,可以用可以不用
public event EventHandler NextPageClick;
public event EventHandler PrePageClick;
public event EventHandler LastPageClick;
public event EventHandler JumpClick;
}

[原创]WinForm分页控件制作