C# 分頁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | 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 取表結構方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /// <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)); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步