winform分页管理
注意:其中可能用到部分自定义的扩展方法,在使用中需自己修改一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 分页管理 /// </summary> public class Paging { #region Property private int pageCount; //总页数 /// <summary> /// 每页数 /// </summary> public int PageSize = 10; /// <summary> /// 总数 /// </summary> public int Total = 0; /// <summary> /// 当前页数 /// </summary> public int PageIndex = 1; public int PageCount { get { if (PageSize <= 0) PageSize = 1; if (Total % PageSize == 0) { pageCount = Total / PageSize; } else { pageCount = Total / PageSize + 1; } return pageCount; } set { pageCount = value; } } #endregion /// <summary> /// 分页操作,name默认为"",否则为操作控件的Name /// </summary> /// <param name="bn">分页控件菜单</param> /// <param name="name">操作类型</param> public void SplitMenu(BindingNavigator bn, string name) { #region Name string tstbPageSize = bn.Items[1].Name; string tstbPageIndex = bn.Items[7].Name; string tslPageCount = bn.Items[8].Name; string tsbFirst = bn.Items[4].Name; string tsbPageUp = bn.Items[5].Name; string tsbPageDown = bn.Items[10].Name; string tsbLast = bn.Items[11].Name; #endregion try { this.PageSize = bn.Items[tstbPageSize].Text.ToString().ToInt(0); this.PageIndex = bn.Items[tstbPageIndex].Text.ToString().ToInt(0); #region action if (name == tsbFirst) PageIndex = 1; else if (name == tsbPageUp) PageIndex--; else if (name == tsbPageDown) PageIndex++; else if (name == tsbLast) PageIndex = PageCount; else if ((name == tstbPageIndex) || (name == tstbPageSize) || (name == "delete")) { int index = bn.Items[tstbPageIndex].Text.ToString().ToInt(1); int size = bn.Items[tstbPageSize].Text.ToString().ToInt(1); if (size == 0) size = 1; if (size > Total) { //if (Total != 0) // size = Total; if (PageIndex * PageSize > Total) PageIndex = 1; } PageSize = size; if (index > PageCount) index = PageCount; PageIndex = index; } //else //{ // //bn.Items[tsbFirst].Enabled = false; // //bn.Items[tsbPageUp].Enabled = false; // //bn.Items[tsbLast].Enabled = false; // //bn.Items[tsbPageDown].Enabled = false; //} #endregion } catch (Exception ex) { Fn.ShowWarning(ex.Message); } #region show if (PageIndex <= 1) { PageIndex = 1; bn.Items[tsbFirst].Enabled = false; bn.Items[tsbPageUp].Enabled = false; } else { bn.Items[tsbFirst].Enabled = true; bn.Items[tsbPageUp].Enabled = true; } if (PageIndex == PageCount) { bn.Items[tsbLast].Enabled = false; bn.Items[tsbPageDown].Enabled = false; } else { bn.Items[tsbLast].Enabled = true; bn.Items[tsbPageDown].Enabled = true; } if (Total == 0) { bn.Items[tsbFirst].Enabled = false; bn.Items[tsbPageUp].Enabled = false; bn.Items[tsbLast].Enabled = false; bn.Items[tsbPageDown].Enabled = false; } bn.Items[tstbPageSize].Text = PageSize.ToString(); bn.Items[tstbPageIndex].Text = PageIndex.ToString(); bn.Items[tslPageCount].Text = "/{" + PageCount.ToString() + "}页"; #endregion } /// <summary> /// 获取当前页 /// </summary> /// <param name="dt">数据源</param> /// <returns></returns> public DataTable GetDataTable(DataTable dt) { DataTable dtTemp = dt.Clone(); int dtCount = dt.Rows.Count; if (dtCount == 0) { return dtTemp; } int start = (PageIndex - 1) * PageSize; int end = PageIndex * PageSize; if (start > dtCount) start = 0; if (end > dtCount) end = dtCount; for (int i = start; i < end; i++) { if (i>dt.Rows .Count ) { continue; } DataRow row = dtTemp.NewRow(); row.ItemArray = dt.Rows[i].ItemArray; dtTemp.Rows.Add(row); } return dtTemp; } }