#region 全局变量 PageIndex PageSize Conntion Count MySqlHelp Sqldb = new MySqlHelp(); QueryHelp db = new QueryHelp(); // 页数 public int PageIndex = 1; //条数 public int PageSize = 1000; //链接语句 public string Conntion = "server=192.168.1.175;user id=root;password=123456;database=txst;"; public int Count { get; set; } #endregion #region 设置按钮首页,尾页,下一页,上一页的可用和不可用属性 /// <summary> ///DownPag.IsEnabled 上一页 ///UpPag.IsEnabled 下一页 ///onePag.IsEnabled 首页 ///Listpag.IsEnabled 尾页 ///<param name="state"></param> void SetState(bool state) { DownPag.IsEnabled = state; UpPag.IsEnabled = state; onePag.IsEnabled = state; Listpag.IsEnabled = state; } #endregion #region 加载 public Selectpag() { InitializeComponent(); string safeSql = "TableTestSelect"; Select_pag_pag.DataContext = db.ToList<tabletset>(safeSql, Conntion); Count = GeiCountPage(); UpPag.IsEnabled = false; onePag.IsEnabled = false; } #endregion #region 首页 private void onePagClick(object sender, RoutedEventArgs e) { PageIndex = 1; List<DbParameter> listpar = new List<DbParameter>{ new MySqlParameter("?pageIndex",PageIndex), new MySqlParameter("?pageSize",PageSize) }; string safeSql = "getpager"; Select_pag_pag.DataContext = db.ToList<tabletset>(safeSql, listpar, Conntion); } #endregion #region 尾页 private void ListpagClick(object sender, RoutedEventArgs e) { PageIndex = GeiCountPage(); List<DbParameter> listpar = new List<DbParameter>{ new MySqlParameter("?pageIndex",PageIndex), new MySqlParameter("?pageSize",PageSize) }; string safeSql = "getpager"; Select_pag_pag.DataContext = db.ToList<tabletset>(safeSql, listpar, Conntion); } #endregion #region 上一页 private void UpPagClick(object sender, RoutedEventArgs e) { SetState(true); PageIndex--; if (PageIndex <= 1) { PageIndex = 1; UpPag.IsEnabled = false; onePag.IsEnabled = false; } List<DbParameter> listpar = new List<DbParameter>{ new MySqlParameter("?pageIndex",PageIndex), new MySqlParameter("?pageSize",PageSize) }; string safeSql = "getpager"; Select_pag_pag.DataContext = db.ToList<tabletset>(safeSql, listpar, Conntion); } #endregion #region 下一页 private void DownPag_Click(object sender, RoutedEventArgs e) { SetState(true); PageIndex++; if (PageIndex >= Count) { PageIndex = Count; DownPag.IsEnabled = false; Listpag.IsEnabled = false; } List<DbParameter> listpar = new List<DbParameter>{ new MySqlParameter("?pageIndex",PageIndex), new MySqlParameter("?pageSize",PageSize) }; string safeSql = "getpager"; Select_pag_pag.DataContext = db.ToList<tabletset>(safeSql, listpar, Conntion); } #endregion #region 测试表 class tabletset { public int ID { get; set; } public string NameTest { get; set; } public string PasswordTset { get; set; } } #endregion #region 总条数 private int GeiCountPage() { string cmdText = "tableByPager"; List<DbParameter> ListPar = new List<DbParameter> { new MySqlParameter("?Count",DbType.Int32){Direction= ParameterDirection.Output} }; Sqldb.ExecuteNonQuery(cmdText, ListPar, Conntion); int PageCount = int.Parse(ListPar[0].Value.ToString()); if (PageCount % PageSize == 0) { PageCount = PageCount / PageSize; } else { PageCount = (PageCount / PageSize) + 1; } return PageCount; } #endregion
普通分页
时间: 2024-10-13 21:01:07