public class PagingHelper { public static DataTable _dt; //每页显示多少条 public static int pageNum = 20; //当前是第几页 public static int pIndex = 1; //对象 //private DataGrid grdList; //最大页数 public static int MaxIndex = 1; //一共多少条 private int allNum = 0; #region 初始化数据 /// <summary> /// 初始化数据 /// </summary> /// <param name="grd"></param> /// <param name="dtt"></param> /// <param name="Num"></param> public void ShowPages(DataTable ds, DataGrid dtList,int Num, TextBox txtCurtPage, Button btnGo) { if (ds == null || ds.Rows.Count == 0) return; if (ds.Rows.Count == 0) return; DataTable dt = ds; _dt = dt.Clone(); //this.grdList = grd; pageNum = Num == 0 ? 20 : Num; pIndex = 1; foreach (DataRow r in dt.Rows) _dt.ImportRow(r); SetMaxIndex(); ReadDataTable((DataGrid)dtList);//繪製 DataGrid if (MaxIndex > 1) { //激活按鈕 abledBtn((TextBox)txtCurtPage, (Button)btnGo); //this.pageGo.IsReadOnly = false; //this.btnGo.IsEnabled = true; } } public void abledBtn(TextBox txtCurtPage, Button btnGo) { txtCurtPage.IsReadOnly = false; btnGo.IsEnabled = true; } #endregion #region 画数据 OK /// <summary> /// 画数据 /// </summary> public void ReadDataTable(DataGrid grdList) { //pIndex = pIndex; try { DataTable tmpTable = new DataTable(); tmpTable = _dt.Clone(); int first = pageNum * (pIndex - 1); first = (first > 0) ? first : 0; //如果总数量大于每页显示数量 if (_dt.Rows.Count >= pageNum * pIndex) { for (int i = first; i < pageNum * pIndex; i++) tmpTable.ImportRow(_dt.Rows[i]); } else { for (int i = first; i < _dt.Rows.Count; i++) tmpTable.ImportRow(_dt.Rows[i]); } grdList.ItemsSource = null; grdList.ItemsSource = tmpTable.DefaultView; tmpTable.Dispose(); } catch { //return "出錯"; //MessageBox.Show("错误"); } finally { //DisplayPagingInfo(); } } #endregion #region 画每页显示等数据 已置于前台 //Old No Used /// <summary> /// 画每页显示等数据 /// </summary> public void DisplayPagingInfo(Button btnPrev, Button btnNext, Button btnFirst, Button btnLast) { if (pIndex == 1) { btnPrev.IsEnabled = false; btnFirst.IsEnabled = false; } else { btnPrev.IsEnabled = true; btnFirst.IsEnabled = true; } if (pIndex == MaxIndex) { btnNext.IsEnabled = false; btnLast.IsEnabled = false; } else { btnNext.IsEnabled = true; btnLast.IsEnabled = true; } //tbkRecords.Text = string.Format("每页{0}条/共{1}条", pageNum, this.allNum); int first = (pIndex - 4) > 0 ? (pIndex - 4) : 1; int last = (first + 9) > MaxIndex ? MaxIndex : (first + 9); //this.grid.Children.Clear(); //獲取列 //for (int i = first; i <= last; i++) //{ // ColumnDefinition cdf = new ColumnDefinition(); // this.grid.ColumnDefinitions.Add(cdf); // TextBlock tbl = new TextBlock(); // tbl.Text = i.ToString(); // tbl.Style = FindResource("PageTextBlock3") as Style; // tbl.MouseLeftButtonUp += new MouseButtonEventHandler(tbl_MouseLeftButtonUp); // tbl.MouseLeftButtonDown += new MouseButtonEventHandler(tbl_MouseLeftButtonDown); // if (i == pIndex) // tbl.IsEnabled = false; // Grid.SetColumn(tbl, this.grid.ColumnDefinitions.Count - 1); // Grid.SetRow(tbl, 0); // this.grid.Children.Add(tbl); //} } #endregion #region 首页 /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnFirst_Method(DataGrid grdList) { pIndex = 1; ReadDataTable(grdList); } /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //private void btnFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // e.Handled = true; //} #endregion #region 上一页 /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnPrev_Method(DataGrid grdList) { if (pIndex <= 1) return; pIndex--; ReadDataTable(grdList); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //private void btnPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // e.Handled = true; //} #endregion #region 下一页 /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnNext_Method(DataGrid grdList) { if (pIndex >= MaxIndex) return; pIndex++; ReadDataTable(grdList); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //private void btnNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // e.Handled = true; //} #endregion #region 未页 /// <summary> /// 未页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnLast_Method(DataGrid grdList) { pIndex = MaxIndex; ReadDataTable(grdList); } /// <summary> /// 未页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //private void btnLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // e.Handled = true; //} #endregion #region 设置最多大页面 /// <summary> /// 设置最多大页面 /// </summary> public void SetMaxIndex() { //多少页 int Pages = _dt.Rows.Count / pageNum; if (_dt.Rows.Count != (Pages * pageNum)) { if (_dt.Rows.Count < (Pages * pageNum)) Pages--; else Pages++; } MaxIndex = Pages; this.allNum = _dt.Rows.Count; } #endregion #region 跳转到多少页 /// <summary> /// 跳转到多少页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnGo_Method(TextBox txtCurtPage, DataGrid grdList) { if (IsNumber(txtCurtPage.Text)) { int pageNum = int.Parse(txtCurtPage.Text); if (pageNum > 0 && pageNum <= MaxIndex) { pIndex = pageNum; ReadDataTable(grdList); } else if (pageNum > MaxIndex) { pIndex = MaxIndex; ReadDataTable(grdList); } } else { txtCurtPage.SelectAll(); txtCurtPage.Focus(); } } #endregion public Regex RegNumber = new Regex("^[0-9]+$"); #region 判断是否是数字 /// <summary> /// 判断是否是数字 /// </summary> /// <param name="valString"></param> /// <returns></returns> public bool IsNumber(string valString) { Match m = RegNumber.Match(valString); return m.Success; } #endregion }
此分頁,實際是將獲取到的DataTable保存在內存中,然後根據頁碼獲取數據,
附DataRow[] 轉 DataTable 取表結構方法
/// <summary> /// DataRow[] 轉 DataTable 取表結構 /// </summary> /// <param name="cloneDt"></param> /// <param name="inDt"></param> /// <returns></returns> public DataTable TransferDto(DataTable cloneDt, DataRow[] inDtRow) { try { DataTable outDt = new DataTable("dt1"); outDt = cloneDt.Clone(); foreach (DataRow row in inDtRow) { outDt.ImportRow(row); } return outDt; #region Others 1 /* DataTable dt=new DataTable(); for (int i= 0; i< dg.Columns.Count; i++) { DataColumn dc = new DataColumn(dg.Columns[i].Name.ToString()); dt.Columns.Add(dc); } for (int i = 0; i< dg.Rows.Count; i++) { DataRow dr = dt.NewRow(); for (int j = 0; j < dg.Columns.Count; j++) { dr[j] = dg.Rows[i].Cells[j].Value.ToString(); } dt.Rows.Add(dr); } return dt; */ #endregion } catch (Exception lo_Ex) { throw new Exception("TransferDto is Fail,ErrMsg:=" + lo_Ex.Message.Substring(0, 200)); } }